0

I have an application that creates an audio file. I need to provide a link that brings up the save dialog when cicked for users to save the file.

I have the download button setup as a link button. From my research it appears that to be able to force bringing up the save dialog, I need to add the following code:

Response.ContentType = "audio/wav";
Response.AppendHeader("Content-Disposition", "attachment; filename=blah.wav");
Response.WriteFile(@"blah.wav");
Response.End();

When I add this to the event handler of the link button, I get the error:

"Line: 4723
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 'RIFF?WAVEfmt '."

So, I decided to try it out by creating an intermediate page that facilitates the download. On the Download linkbutton event handler, I added a Respose.Redirect to this intermediate page and on the Page_Load of the page, I added the code snippet above. When setup this way, IE, blocks the download by saying:

"To help protect your security, Internet Explorer blocked his site from downloading files to your computer. Click here for options" When I click and select "Download file", I do not get the Save dialog.

My questions: 1. Can I eliminate the intermediate page? 2. If I can't how do I not have IE prompt the user for action?

Nick
  • 7,475
  • 18
  • 77
  • 128

4 Answers4

2

Maybe your Response contains something before hitting your code, did you try a Response.Clear() before outputting anything?

Also, there shouldn't be any Response.Write after your Response.End().

This would fix the first error and would eliminate the need for an intermediate page.

I already done this before so I don't see a limitation.

Try to find what's modifying the Response.

Thanks

1
protected void Button1_Click(object sender, EventArgs e)
{
    String FileName1 = "test.mp3";

    String FilePath1 = "~/audio/test.mp3";


    String FileName = "FileName1";
    String FilePath = FilePath1;
    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
    response.ClearContent();
    response.Clear();
    response.ContentType = "text/plain";
    response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
    response.TransmitFile(FilePath);
    response.Flush();
    response.End();
}
<asp:Button ID="Button1" runat="server" Text="DownLoad" 
        onclick="Button1_Click" />
Tisho
  • 8,320
  • 6
  • 44
  • 52
Ese Ochuko
  • 11
  • 2
1
Response.Clear();
            Response.ContentType = "audio/x-wav";
            Response.AddHeader("Content-Disposition", "attachment;filename=blah.wav");
            Response.WriteFile(@"blah.wav");
            Response.Flush();
            Response.End();

I believe you were missing two parts, the Clear() and the Flush().

I've used this exact code before.

As a note, you may need to use Response.BinaryWrite instead of Response.WriteFile.

Clarence Klopfstein
  • 4,682
  • 10
  • 33
  • 47
0

Just a guess, but your problem could be due to some MIME type not set on the server.

Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166
  • Check here for MIME types http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/cd72c0dc-c5b8-42e4-96c2-b3c656f99ead.mspx?mfr=true – Myra Dec 27 '09 at 21:00
  • Well, if I go to the intermediate page directly (for testing, opened this pge directly in the browser), everything works as expected. I see the save dialog and can download/open the wav file. This makes me think that this MIME type is set on the server. No? – Nick Dec 27 '09 at 21:02
  • You can understand it this way: Registry.GetValue(string.Format(@"HKEY_CLASSES_ROOT\{0}", Path.GetExtension(filePath)); If this method returns the value of known file type,then this means your machine has signed for that mime type,which means signed for iis. – Myra Dec 27 '09 at 21:08