Getting Path from <input type="file"> & Attach to Email
Using an <input type="file" name="fileUpload">
I can get the file path by using Request.MapPath and storing it in a string. But when I do:
string file =开发者_运维百科 Request.MapPath(Request.Form["fileUpload"]);
Attachment.Add(new Attachment(file));
I get the 'Could not find a part of the path' error. What an I missing in getting the file or attaching the file to the MailMessage object?
I believe you cannot do that because the file has not been written to the server disk; that is, it is buffered in memory. Try this:
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);
}
精彩评论