[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 479
  • Last Modified:

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]);
0
1jaws
Asked:
1jaws
  • 2
  • 2
1 Solution
 
Jacques Bourgeois (James Burger)Commented:
System.IO.File.Copy ( result, "<path and name of the destination file on the user machine>" );

Open in new window

0
 
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
 
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

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now