Download File to client's local harddrive

Last post 05-12-2008 11:11 AM by booler. 6 replies.

Sort Posts:

  • Download File to client's local harddrive

    05-12-2008, 6:17 AM

    Hi Friends,

    I have to download a file on local harddrive from Server. without asking Browse Path to Save that file on Local Machine.  

    i am usung that code

    // Get the physical Path of the file(test.doc)

    string filepath = Server.MapPath("~\\App_Data\\Temp.mdb");

    // Create New instance of FileInfo class to get the properties of the file being downloaded

    FileInfo file = new FileInfo(filepath);

    // Checking if file exists

    if (file.Exists)

    {

    // Clear the content of the response

    Response.ClearContent();

    // LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header

    Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

    // Add the file size into the response header

    Response.AddHeader("Content-Length", file.Length.ToString());

    // Set the ContentType

    Response.ContentType = ReturnExtension(file.Extension.ToLower());

    // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)

    Response.TransmitFile(file.FullName);

    // End the response

    Response.End();

    }

    Thanks in Advance

    Email Id: dhemittal@gmail.com, dheeraj_mittal02@yahoo.co.in, dheeraj.mittal@sagarsolutions.com

     

    Filed under:
  • Re: Download File to client's local harddrive

    05-12-2008, 6:43 AM
    Answer
    • Star
      14,342 point Star
    • booler
    • Member since 08-15-2005, 10:22 AM
    • Brighton, England
    • Posts 2,205

    This can't be done. The 'save file' dialog is a client-side browser behaviour, not a function of the server-side code, and one I'm afraid you have no control over. 

    If it's an intranet app, you could use an activex control, or you could look into using a signed java applet, but in general, trying to find ways to circumvent built-in browser behaviour is pretty bad practice and best avoided.
     

  • Re: Download File to client's local harddrive

    05-12-2008, 6:51 AM
    • Participant
      1,358 point Participant
    • Sudheendra B
    • Member since 04-25-2008, 11:06 AM
    • India
    • Posts 286

     Hi,

    try with this code,

                            string FileName = Server.MapPath("~\\App_Data\\Temp.mdb");
                            string strFilename = FileName.Substring(0, strSongFileName.LastIndexOf("."));
                            Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
                            Response.TransmitFile(Server.MapPath("~/Upload/" + FileName));
                            Response.End();

    I think this will solves your problem.

    Mark this as answer, if it solves your problem.

    Thank you. 

    Thank you. Yes


    Please don't forget to mark the post as Answer, that helps you.
  • Re: Download File to client's local harddrive

    05-12-2008, 7:26 AM
    • Star
      14,342 point Star
    • booler
    • Member since 08-15-2005, 10:22 AM
    • Brighton, England
    • Posts 2,205

    Sudheendra B: what is the purpose of this:

     Response.TransmitFile(Server.MapPath("~/Upload/" + FileName));

    Confused 

  • Re: Download File to client's local harddrive

    05-12-2008, 7:43 AM
    • Participant
      1,358 point Participant
    • Sudheendra B
    • Member since 04-25-2008, 11:06 AM
    • India
    • Posts 286

     Hello,

    When I run  this code, the File Download Dialog Opens: This is used to open the file download dialog box.

    Thank you. 

     

    Thank you. Yes


    Please don't forget to mark the post as Answer, that helps you.
  • Re: Download File to client's local harddrive

    05-12-2008, 10:45 AM
    dear this solution still asking Browse Path to Save that file on Local Machine.
  • Re: Download File to client's local harddrive

    05-12-2008, 11:11 AM
    Answer
    • Star
      14,342 point Star
    • booler
    • Member since 08-15-2005, 10:22 AM
    • Brighton, England
    • Posts 2,205

     Yes, I'm afraid so. What you're trying to do would allow a server to store a file on a user's hard disk without them asking for it. You can imagine why this might not be a good thing if it were allowed...

    I'm afraid the 'save file' dialog is something you are going to have to work with- as there really is no good workaround.

Page 1 of 1 (7 items)