2011-05-27 82 views
0

使用<input type="file" name="fileUpload">我可以通过使用Request.MapPath并将其存储在字符串中来获取文件路径。但是,当我做的:从<input type =“file”>获取路径&附加到电子邮件

string file = Request.MapPath(Request.Form["fileUpload"]); Attachment.Add(new Attachment(file));

我得到“找不到路径的一部分”的错误。在获取文件或将文件附加到MailMessage对象时缺少什么?

+0

这是一个安全问题。 – 2011-05-27 03:40:59

+0

我不知道你的应用程序,但通常客户端(浏览器)和服务器(webapp)在不同的主机上。您无法直接访问其他主机上的文件。特殊的上传文件控制用于此目的。 – artplastika 2011-05-27 03:41:51

+0

@Alex R. - 如果是这样,是否有一个属性,我应该改变我的控制器操作? – 2011-05-27 03:44:40

回答

3

我相信你不能这样做,因为该文件没有被写入服务器磁盘;也就是说,它被缓存在内存中。试试这个:

var destination = Path.GetTempFileName(); // you should probably replace this with a directory the IIS Worker Process has write permission to 
try { 
Request.Files[0].SaveAs(destination); 

Attachment.Add(new Attachment(destination)); 
// Send attachment 
} finally { 
File.Delete(destination); 
} 
相关问题