download file

I have this code below that gets the folder and the file name from directory ok... now, I need user to be able to download the file wherever they wish to download on their pc... so, what I need to do next?

string folder = Server.MapPath("/emailAttachments/" + lblattachment.Text.ToString());
            string[] filePaths = Directory.GetFiles(folder);
            string result = Path.GetFileName(filePaths[0]);
1jawsAsked:
Who is Participating?
 
SriVaddadiCommented:
System.IO.FileInfo targetFileInfo = new System.IO.FileInfo(filePaths[0]);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename="+targetFileInfo.Name);
Response.AddHeader("Content-Length", targetFileInfo.Length.ToString());
Response.ContentType = "application/octet-stream"
Response.WriteFile(targetFileInfo.FullName)
Response.End();



0
 
Jacques Bourgeois (James Burger)PresidentCommented:
System.IO.File.Copy ( result, "<path and name of the destination file on the user machine>" );

Open in new window

0
 
1jawsAuthor Commented:
James, yours doesnt work... SriVaddadi, I got this error..

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near 'PK'.
0
 
1jawsAuthor Commented:
I do that code inside the ItemCommand(object sender, DetailsViewCommandEventArgs e)  for LinkButton clicked....
0
 
SriVaddadiCommented:
Hope you could resolve the error or let me know
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.