ASP.NET - PDF downloading in .aspx format instead of PDF - asp.net

I created a PDFViewer form in ASP.NET, and it works fine within the app. In the last part of the code, I use these lines to generate the PDF file from information I used previously (I'm using the PDFSharp library):
MemoryStream stream = new MemoryStream();
pdf.Save(stream, false);
response.Clear();
response.ContentType = "application/pdf";
response.AddHeader("content-length", stream.Length.ToString());
response.BinaryWrite(stream.ToArray());
response.Flush();
stream.Close();
response.End();
The file looks good, I see all the PDF browser options to print, zoom, downlad, etc.
The issue I'm having is, when I click on the download button, it wants to download the PDF as "PDFname.aspx", instead of "PDFname.pdf", although I am specifying the content type is "application/pdf". Which other thing could be missing?

Related

ASP.NET how to return pdf file as response without changing response header

I want to return a pdf file as response to some button click.
I succeeded to send the pdf file, but when i try to save it via the browser, it won't let me save it as a .pdf file (but as .aspx file)
here's the code:
Dim myWebClient As WebClient = New WebClient()
Dim myDataBuffer As Byte() = myWebClient.DownloadData(LocalImageURL) ' LocalImageURL is some path to a pdf file
Response.ContentType = "application/pdf"
Response.BinaryWrite(myDataBuffer)
Response.Flush()
Response.End()
if I am adding also the following line before writing the byte array:
Response.AddHeader("content-disposition", "attachment;filename=report.pdf")
it does the trick, but the problem is that the page remains stuck (looks like it still waits for server response to come)
This has been asked and answered before. Details are at the link below:
asp.net VB.net file download handler not work properly

Displaying PDF from Memory Stream to browser

I am trying to display a pdf created dynamically and display it in the browser, but I am getting the below characters( a whole lot of them)
Im using iTextSharp
\�(l�x�)�(�)���g���29��2�`C�B�Wa���[�(�o��x��3�J :k��v�os�R
The code which I used to create the pdf does work when sending it by email as an attachment.
Maybe im wrong on displaying it ?
....
doc.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=labtest.pdf");
Response.Buffer = true;
stream2.WriteTo(Response.OutputStream);
Response.End();
code above is in a controller method
Answered in comments of question.
Credit to Chris Haas
AJAX in its general form is text-based which is why you are seeing a
text-representation of a PDF. You need to switch to binary processing
if you want to work with binary data but that's a whole different
question. See this for an intro html5rocks.com/en/tutorials/file/xhr2

Displaying a PDF on a website

I require some assistance, I have a pdf file that is located on a ftp location.
What i want to do, is on a button click i want to retrieve the pdf file and display it on a new tab.
Please assist on how to accomplish this task.
Thanks in advance.
One approach would be to make a web request to download the file and then pass the download stream along to the ASP.NET response stream. I haven't compiled or executed this code, but it might look something like this:
WebRequest request = HttpWebRequest.Create("ftp://ftp.yoursite.com/yourfile.pdf");
request.Credentials = new NetworkCredential("ftpuser","ftppassword");
// Get the response
WebResponse response = request.GetResponse();
StreamReader responseStream = new StreamReader(response.GetResponseStream());
// Send the response directly to output
Response.ContentEncoding = responseStream.CurrentEncoding;
Response.ContentType = "application/pdf";
Response.Write(responseStream.ReadToEnd());
Response.End();
Just use the HTML anchor tag ("a" tag). In the href attribute put the FTP address of the PDF file (eg. ftp://example.com/file.pdf). To open in a new window you should also specify a target attribute with the value "_blank".
Example:
<a href='ftp://example.com/file.pdf' target='_blank'>Click here to open the PDF file</a>

PDF from content of asp.net page

I'm trying to create a pdf of the content on a page ("returnsPage.aspx?id="returnId) and allow the user to download this directly when clicking the button.
However in my onClick method I have the following code:
lnkLoadPDF.CommandArgument = "/returns/returnsPage.aspx?id="+returnId.ToString();
string virtualPath = lnkLoadPDF.CommandArgument;
string fileName = System.IO.Path.GetFileName(virtualPath);
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.WriteFile(virtualPath);
Response.ContentType = "";
Response.End();
Response.Redirect("/returns/returnsPage.aspx?id="+returnId);
which returns this error:
'/returns/returnsPage.aspx?id=23' is not a valid virtual path.
Can anyone please tell me what I'm doing wrong?
Thanks!
In order to turn a webpage into a pdf, you must convert it to pdf on the server. In order to do that, you must have a program on the server that can do that for you.
I've tried a variety of webpage-to-pdf converters and one of the better ones is a free, open source program called wkhtmltopdf.
After you create the pdf, you can either redirect the user to the newly created pdf (discouraged), or prompt them to download it with a savefile dialog.
If you get stuck, just search for wkhtmltopdf on stackoverflow or post another question.
You can't send a file to the client and redirect him to a new location during the same request. You also can't create a PDF from a webpage without some kind of component that converts the HTML into a PDF, it's (quite a bit) more tricky that what I think you're trying to attempt.
As for your exception, are you sure returnsPage.aspx exists? :)

Opening file with different name shown to user

I have a directory with a lot of files in it named: file001.pdf, file002.pdf etc.
Now I want a linkbutton in asp.net witch opens one of the above files with an other name!
So for example the linkbutton show's and opens when clicked the pdf: newname.pdf.
Is this possible in asp.net? I only want the client to see the newname.pdf and I want the file001.pdf remains on the server.
You could try reading in the file into a Byte array (or as a stream) and then output the file with a different name?
Byte[] myFile = System.IO.File.ReadAllBytes("~/MyPdf.pdf");
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "filename=NEW_NAME.pdf");
Response.OutputStream.Write(myFile, 0, myFile.Length);
Response.Flush();
Note, not tested this. But it should point you in the right direction...

Resources