2015-12-22 57 views
0

我正在创建一个具有一种形式的简单页面。它的代码如下所示:获取电子邮件附件的文件上传umbraco

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage 
@using System.Net; 
@using System.Net.Mail; 
@{ 
    if(IsPost) 
    { 
     //Way 1: to get attachment 
     var fileSavePath = ""; 
     var uploadedFile = Request.Files[0];//Here not getting file name 
     var fileName = Path.GetFileName(uploadedFile.FileName); 
     fileSavePath = Server.MapPath("~/media/" + fileName); 
     uploadedFile.SaveAs(fileSavePath); 
     FileInfo info = new FileInfo(fileSavePath); 
     string[] ext = fileName.Split('.'); 

     //Way 2 : 
     var a = Request["fluld"];//Getting file name only 
     var b = Request.Files;//Getting null here 

     string d = Path.GetFullPath(Request["fluld"]); 
     string c = string.Empty; 

     try 
     { 
      MailMessage mail = new MailMessage(); 
      SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); 

      mail.From = new MailAddress("[email protected]"); 
      mail.To.Add("[email protected]"); 
      mail.Subject = "Test Mail"; 
      mail.Body = "This is for testing SMTP mail from GMAIL"; 

      System.Net.Mail.Attachment attachment; 
      attachment = new System.Net.Mail.Attachment("filepath"); 
      mail.Attachments.Add(attachment); 

      SmtpServer.Port = 587; 
      SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "******"); 
      SmtpServer.EnableSsl = true; 

      //SmtpServer.Send(mail); 
      //MessageBox.Show("mail Send"); 
     } 
     catch (Exception ex) 
     { 
      throw new Exception(ex.Message.ToString()); 
     } 
    } 
} 
<form method="post"> 
    <input type="file" name="fluld" id="fluld" /> 
    <input type="submit" value="Sub"/> 
</form> 

我无法使用此Request.Files获取电子邮件附件的文件。帮我解决这个问题。任何我需要添加的东西?此代码位于umbraco的模板中。

感谢

蒂帕

回答

0

你的形式必须是 '多/表单数据'

<form enctype="multipart/form-data" method="post"> 
    <input type="file" name="fluld" id="fluld" /> 
    <input type="submit" value="Sub"/> 
</form>