Question

I am writing a webapplication in ASP.net.

I am trying to make a file dialog box appear for downloading something off the server.

I have the appropriate file data stored in a variable called file.

File has fields: FileType - The MIMEType of the file
FilePath - The server-side file path

Here's the code so far:

Response.Clear();
Response.ContentType = file.FileType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" +     GetFileName(file));
Response.TransmitFile(file.FilePath) ;
Response.End(); 

GetFileName is a function that gets me the filename from an attachment object. I only store the path.

The above code is in a function called "Download_Clicked" that is an event that triggers on click. The event is mapped to a LinkButton.

The problem is that when I run the above code, nothing happens. The standard dialog box does not appear.

I have attempted the standard trouble-shooting such as making sure the file exists, and ensuring the path is correct. They are both dead on the mark.

My guess is that because my machine is also the server, it may not be processing properly somehow.

Thanks in advance.

Edit 1: Attempted putting control onto another page, works fine.

Edit 2: Resolved issue by removing control from AJAX Update Panel.

Was it helpful?

Solution 4

See edit on initial post.

Removed Ajax Update Panel to resolve the error. The panel was stopping the post back to the server.

For more info, see Cris Valenzuela's comment.

OTHER TIPS

I've found another to do this without removing the update panel. Place the code below in your page load and you'll now be able to use that button to trigger a download.

ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(Button);

Use Response.WriteFile() instead.

Also, don't use Response.End()! This aborts the thread. Use Response.Flush(); Response.Close();

Try changing

Response.AppendHeader("Content-Disposition", "attachment; filename=" +     GetFileName(file));

To

Response.AppendHeader("Content-Disposition", "attachment; filename=" +     Path.GetFileName(GetFileName(file))));

If that doesn't work, you can always use Response.BinaryWrite or Resonse.Write to stream the file to the web browser Here is how transmit the file using Response.Write or Response.BinaryWrite. Put these functions in a library somewhere then call them as needed

 public void SendFileToBrowser(String FileName, String MIMEType, String FileData)
    {
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
        Response.ContentType = MIMEType;
        Response.Buffer = true;
        Response.Write(FileData);
        Response.End();
    }

    public void SendFileToBrowser(String FileName, String MIMEType, Byte[] FileData)
    {
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
        Response.ContentType = MIMEType;
        Response.Buffer = true;
        Response.BinaryWrite(FileData);
        Response.End();
    }

Then somewhere you call these functions like so

SendFileToBrowser("FileName.txt", "text/plain", "Don't try this from an Update Panel. MSAjax does not like it when you mess with the response stream.");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top