download file

Posted on 2011-10-14
Last Modified: 2012-06-27
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]);
Question by:1jaws
    LVL 40

    Expert Comment

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

    Open in new window

    LVL 16

    Accepted Solution

    System.IO.FileInfo targetFileInfo = new System.IO.FileInfo(filePaths[0]);
    Response.AddHeader("Content-Disposition", "attachment; filename="+targetFileInfo.Name);
    Response.AddHeader("Content-Length", targetFileInfo.Length.ToString());
    Response.ContentType = "application/octet-stream"


    Author Comment

    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'.

    Author Comment

    I do that code inside the ItemCommand(object sender, DetailsViewCommandEventArgs e)  for LinkButton clicked....
    LVL 16

    Expert Comment

    Hope you could resolve the error or let me know

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Suggested Solutions

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now