Rank: Advanced Member Groups: Member
Joined: 1/31/2008 Posts: 32 Points: 96 Location: UK
|
Hi,
Can anyone help me.
In my application I need to allow the user to send mails with multiple attachments.
For that I have a FileUpload control and a button in my page. After browsing a file from file upload control user will hit the button, so that I am adding FilePath
information in a viewstate.
When user clicks on the sendMail button I am taking the filepaths that are available in viewstate and giving a statement like DataTable dt = (DataTable)ViewState["AttachedFiles"];
foreach (DataRow drow in dt.Rows)
//Attach the file
message.Attachments.Add(new Attachment(drow["FilePath"].ToString()));
//in the filepath I have the fullpath of the file ex:FileUpload.PostedFile.FileName
when I execute the application I am getting an exception called UnauthrizedAccessException. I am able to send a single attachment by using
message.Attachments.Add(new Attachment(FileUpload.PostedFile.InputStream,FileUpload.FileName));
but my requirement is allowing the user to attach multiple files.
Any ideas will be appreciated.
|
Rank: Advanced Member Groups: Member
Joined: 3/3/2008 Posts: 79 Points: 237 Location: India
|
Sure, I'll put it in VB so that you at least have to translate it into C# :)
You can simply store the file info like so: Session("FileBytes") = fileUpload.FileBytes()
Session("FileName") = fileUpload.FileName
And then you can access that in a later request like so: Dim fileName As String = CType(Session("FileName"), String) Dim fileBytes As Byte() = CType(Session("FileBytes"), Byte())
Dim memoryStream As New System.IO.MemoryStream(fileBytes)
And then you can proceed as you did before:
message.Attachments.Add(new Attachment(memoryStream, fileName))
In C#, you'll just have to modify the syntax slightly, and cast the C# way ( which I prefer ).
|