1

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.

wanderingloki
  • 65
  • 1
  • 4
  • Is the mime type known by IIS? have you tried using "application/octet-stream"? – Gary.S Oct 17 '11 at 18:34
  • I've attempted octet-stream MIME type and it still doesn't go. – wanderingloki Oct 17 '11 at 19:56
  • 2
    Ajax Update panels don't play well with the FileUpload control or downloading files using Response. MSAjax library has built in security measures which will throw an error if you modify the Response stream. A common technique to download dynamically generated files is to temporarily store the file data in a session variable then redirect the user to a page which specifically looks at that session variable on page load and sends the file to the browser. You can also do this with an http handler or simply leave the file upload/download controls outside of the update panels – Cris Valenzuela Oct 18 '11 at 16:20

4 Answers4

3

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);

James
  • 5,812
  • 5
  • 26
  • 30
0

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.");
Cris Valenzuela
  • 372
  • 3
  • 9
0

Use Response.WriteFile() instead.

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

Leon
  • 3,311
  • 23
  • 20
  • I attempted using Response.WriteFile. The unusual thing is that I took the control and threw it onto another page, and it worked fine. But it won't work on the initial page I put it onto. – wanderingloki Oct 17 '11 at 18:32
  • `Response.ContentType = file.FileType;` - the content type is a MIME content type, *not* file type. You can google for a list of common content types to match the file you're outputting to the client. Check that your "broken" page isn't sending any headers - once those are sent to the client, you can't re-send them. – Leon Oct 17 '11 at 18:45
  • The file.FileType is the MIME type of the file. Its only used for outputting. This is the only function that sends something to the client, there is nothing else on the page that sends stuff. – wanderingloki Oct 17 '11 at 19:55
0

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.

wanderingloki
  • 65
  • 1
  • 4