views:

36

answers:

2

Hi,

I have a "dummy" page to force the "SaveAs" pop-up when they click a link to download a document.

the request for that site is ajax, and the site is called. I can se that it got all the right params, but when it comes to this part nothing happens.

var filepath = Request["filepath"];

//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";
//Get the physical path to the file.
string FilePath = MapPath(filepath);
Response.AppendHeader("content-disposition", "attachment; filename=" + FilePath);
//Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath);
esponse.End();

with firebug, I can see that i tries to response something, if I look at the header.

Server ASP.NET Development Server/10.0.0.0
Date Tue, 14 Sep 2010 12:51:02 GMT
X-AspNet-Version 4.0.30319
Content-Disposition attachment; filename=D:\APPLICATIONS\InfoomFilm.pdf
Cache-Control private
Content-Type Application/pdf
Content-Length 785693
Connection Close

but if I look at the response, it looks like

%PDF-1.3
%���������
4 0 obj
<< /Length 5 0 R /Filter /FlateDecode >>
stream
x��ْ��u���)pي k��w�,��($þp袇,rz�lR���z�~��+8�/��$�Ld�P�,rȑ"�'��%d���s�ע,�mi��}9�}Wt�n[C[m���P�WqW|��CU<�����s{m[���f����a�

and so on for the next 785600 characters

+1  A: 

i think you can do something like this

Response.ContentType = "Application/pdf";

Response.AppendHeader("content-disposition", "attachment; filename=" + FilePath);

Response.TransmitFile( Server.MapPath(filepath) );

Response.End();

and that may work - this is what I have done and is thanks to Rick Strahl's blog. Personally Id determine the Content type programatically using the MIME type so that the code isnt specifi to PDF but thats just me :)

PaulStack
+1  A: 

I do something similar, and it works for me:

Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.TransmitFile(Server.MapPath(FilePath));
Response.End();
EJC