使用<input type="file" name="fileUpload">
我可以使用Request.MapPath获取文件路径并将其存储在字符串中。但是当我这样做时:
string file = Request.MapPath(Request.Form["fileUpload"]);
Attachment.Add(new Attachment(file));
我得到'找不到路径的一部分'错误。在获取文件或将文件附加到MailMessage对象时我缺少什么?
答案 0 :(得分: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);
}