Question

I've put a FileUpload control onto my form. When client browses for a file and selects one I want to use that file as an attachment to my mail message. For this purpose I write:

 Attachment attachment = new Attachment(fileUpload1.FileName);
 mail.Attachments.Add(attachment); 

I get an error that says:"Could not find file 'C:\Windows\SysWOW64\inetsrv\Water lilies.jpg'." The thing is the path to the file is different from the path in the client. How can I attach a file that is on the client's machine to a mail message?

OTHER TIPS

Server.MapPath should fix your problem.

 Attachment attachment = new Attachment(Server.MapPath(fileUpload1.FileName));

The FileName property gives you just that - the name of the file, no path included. You're seeing inetsrv in the path because that's IIS' working directory. You will probably want to utilize the PostedFile property, which will handle saving for you:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfile.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top