473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Forced Download of PDF

Dear Groups,

I am attempting to allow the end user to save or open a pdf document
from the server. However, when the file is downloaded it is not a
pdf, but rather nonsensical garbage. Also, if attempting to save the
file, it only gives the option of file type "text document".

I've run the app on other machines and am fresh out of ideas.

Thanks in advance,

Darren

Code as follows:

Session["FilePath"]=Server.MapPath("../docPDF/") +
e.CommandName.ToString();

Response.ContentType="application/pdf";

Response.AddHeader( "content-disposition","attachment; filename=" +
Session["FilePath"].ToString());

FileStream sourceFile = new
FileStream(@Session["FilePath"].ToString(), FileMode.Open);
long FileSize;
FileSize = sourceFile.Length;
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)sourceFile.Length);
sourceFile.Close();

Response.BinaryWrite(getContent);
Nov 19 '05 #1
3 913
Try putting Response.Clear() at the beginning of your code, and
Response.End() after it to make sure nothing except the PDF file is being
output.
Here's more info:
http://SteveOrr.net/articles/easyuploads.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"Darren" <da****@yellowpencil.com> wrote in message
news:32**************************@posting.google.c om...
Dear Groups,

I am attempting to allow the end user to save or open a pdf document
from the server. However, when the file is downloaded it is not a
pdf, but rather nonsensical garbage. Also, if attempting to save the
file, it only gives the option of file type "text document".

I've run the app on other machines and am fresh out of ideas.

Thanks in advance,

Darren

Code as follows:

Session["FilePath"]=Server.MapPath("../docPDF/") +
e.CommandName.ToString();

Response.ContentType="application/pdf";

Response.AddHeader( "content-disposition","attachment; filename=" +
Session["FilePath"].ToString());

FileStream sourceFile = new
FileStream(@Session["FilePath"].ToString(), FileMode.Open);
long FileSize;
FileSize = sourceFile.Length;
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)sourceFile.Length);
sourceFile.Close();

Response.BinaryWrite(getContent);

Nov 19 '05 #2
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "Application/pdf";
try
{
Response.WriteFile( MapPath( "Sample.pdf") );
Response.Flush();
Response.Close();
}
catch
{
Response.ClearContent();
}
"Darren" wrote:
Dear Groups,

I am attempting to allow the end user to save or open a pdf document
from the server. However, when the file is downloaded it is not a
pdf, but rather nonsensical garbage. Also, if attempting to save the
file, it only gives the option of file type "text document".

I've run the app on other machines and am fresh out of ideas.

Thanks in advance,

Darren

Code as follows:

Session["FilePath"]=Server.MapPath("../docPDF/") +
e.CommandName.ToString();

Response.ContentType="application/pdf";

Response.AddHeader( "content-disposition","attachment; filename=" +
Session["FilePath"].ToString());

FileStream sourceFile = new
FileStream(@Session["FilePath"].ToString(), FileMode.Open);
long FileSize;
FileSize = sourceFile.Length;
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)sourceFile.Length);
sourceFile.Close();

Response.BinaryWrite(getContent);

Nov 19 '05 #3

Hi Steve,

Thanks so much for your most prompt reply.

When I added the Response.Clear() and Response.End(), the following
resulted:

1) Registers as type Adobe Acrobat Control for Active X
2) Allows me to save, but the default document name is "Default", and
file type is Adobe Acrobat Document
3) When the user chooses to open rather than save, it ignores the
request and shows the save dialogue box.

Any ideas?

Thanks again,

Darren

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: dany | last post by:
I want a forced PDF-download but all that header-stuff is new for me.... I wrote a little asp-script: <% Filename = "download.pdf" response.addheader "content-disposition","attachment;...
1
by: dany | last post by:
Wrote a script (tried to;) which should force to download a pdf-file, but it doesn't work on every browser. What should be changed ? SCRIPT: --------- <% Dim Stream Dim Contents
2
by: Gary D. Rezek | last post by:
Hi All, First of all I'm mostly an MS Access guy (and novice learning MSSQL guy) and put on this project because of personnel changes, etc and because of db experience. I've only been doing this...
1
by: d. | last post by:
I coded a simple asp-script to force a file-download. (ex. dummy.doc) In IE6, NS4.7 and Opera 7 everything works fine and the file can be easily downloaded... But NS7 renames the filename in...
22
by: MyndPhlyp | last post by:
I am pushing a file download to the client. The ASP used to generate and push the file is triggered by an <a target="_blank" href="something">. The VBScript uses Response.Clear...
1
by: darren | last post by:
Dear Groups, I have a hosted application which allows subscribers to purchase PDF documents. Once purchased, these documents are accessible from a user private library to be downloaded at will....
13
by: Mickey | last post by:
Hi all, Currently I use a timestamp to log users out after 15 minutes of inactivity. However I also need to log a user out if they have just left the page. I need to do this because I store...
2
by: comp.lang.php | last post by:
if ($forceDownload) { // HANDLE FORCED DOWNLOAD $dlGen =& new DownloadGenerator($fullFilePath); $negativeIndex = $dlGen->generateForceDownloadHeaders(); $willDeleteTemp =...
14
by: Simon Rigby | last post by:
Hi folks, I appreciate that this topic of forced downloads has been discussed in various threads but my situtation is a little different to those that I have come across and am experiencing a...
12
by: comp.lang.php | last post by:
index.php: // STUFF // STEP 1: imagecreatetruecolor ONLY IF GD 2.0+ SUPPORTED AND FOUND if ($this->isSuccessful && !$hasMogrified && $image && !$newImage &&...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.