473,769 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Response.WriteF ile help please

I want to serve a PDF right to a web page (cannot link browser directly
to PDF file). Stumbled across Response.WriteF ile this morning. On my
machine (XP Pro) this worked fine:

private void Page_Load(objec t sender, System.EventArg s e)
{
string filePath = "c:\\somepath\\ some.pdf";
Response.Conten tType = "Applicatio n/pdf";
Response.WriteF ile(filePath);
}

Hit the page in Firefox, PDF loads right up. Hit it in IE, nothing.
Just a broken puzzle icon.

Further, one of the developers on my team did the exact same thing and
it doesn't work at all. IE prompts him to save or open, which will save
the PDF (but it opens as a .aspx so he gets PDF gibberish in VS.NET if
he chooses open), and in Firefox he gets a message about the file being
corrupt.

The eventual goal is to do the same with a file on another computer (not
on the web server), so the questions are:

1)Why isn't it working properly in IE?
2)Why would it work on one machine and not another?
3)Any other tips regarding this?

Thank you very much in advance.

Scott.
--

_______________ _______________ ______________
Scott C. Reynolds - Tales From the SharpSide
http://www.scottcreynolds.com
sc***@scottcrey nolds.com

*****Get your SharpSide Swag!******
http://www.cafepress.com/sharpside/
*************** *************** *****
Nov 19 '05 #1
8 5778
Use this code to set the filename so it won't be downloaded as an aspx file.

Response.AddHea der("Content-Disposition", "inline;filenam e=test.pdf")

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"Scott C. Reynolds" <sr*********@no email.nospam> wrote in message
news:e3******** ********@TK2MSF TNGP10.phx.gbl. ..
I want to serve a PDF right to a web page (cannot link browser directly to
PDF file). Stumbled across Response.WriteF ile this morning. On my machine
(XP Pro) this worked fine:

private void Page_Load(objec t sender, System.EventArg s e)
{
string filePath = "c:\\somepath\\ some.pdf";
Response.Conten tType = "Applicatio n/pdf";
Response.WriteF ile(filePath);
}

Hit the page in Firefox, PDF loads right up. Hit it in IE, nothing. Just
a broken puzzle icon.

Further, one of the developers on my team did the exact same thing and it
doesn't work at all. IE prompts him to save or open, which will save the
PDF (but it opens as a .aspx so he gets PDF gibberish in VS.NET if he
chooses open), and in Firefox he gets a message about the file being
corrupt.

The eventual goal is to do the same with a file on another computer (not
on the web server), so the questions are:

1)Why isn't it working properly in IE?
2)Why would it work on one machine and not another?
3)Any other tips regarding this?

Thank you very much in advance.

Scott.
--

_______________ _______________ ______________
Scott C. Reynolds - Tales From the SharpSide
http://www.scottcreynolds.com
sc***@scottcrey nolds.com

*****Get your SharpSide Swag!******
http://www.cafepress.com/sharpside/
*************** *************** *****

Nov 19 '05 #2
Steve C. Orr [MVP, MCSD] wrote:
Use this code to set the filename so it won't be downloaded as an aspx file.

Response.AddHea der("Content-Disposition", "inline;filenam e=test.pdf")

doing that makes IE prompt for save/open, if you hit open, it prompts
again for save/cancel. it breaks the functionality in firefox.

Is there a better way, assuming I can't link directly to the PDF?

--

_______________ _______________ ______________
Scott C. Reynolds - Tales From the SharpSide
http://www.scottcreynolds.com
sc***@scottcrey nolds.com

*****Get your SharpSide Swag!******
http://www.cafepress.com/sharpside/
*************** *************** *****
Nov 19 '05 #3
Hello Scott,

You will want to make sure that you clear everything before you write out.

[C#]
Response.ClearC ontent();
Response.ClearH eaders();
Response.Conten tType = "applicatio n/pdf";
Response.AddHea der("Content-disposition", "inline;filenam e=test.pdf");

Response.WriteF ile(file);
Response.Flush( );
Response.End();

I'm doing it exactly this way, and it works no matter where I run it from
(or which browser).

--
Matt Berther
http://www.mattberther.com
Steve C. Orr [MVP, MCSD] wrote:
Use this code to set the filename so it won't be downloaded as an
aspx file.

Response.AddHea der("Content-Disposition", "inline;filenam e=test.pdf")

doing that makes IE prompt for save/open, if you hit open, it prompts
again for save/cancel. it breaks the functionality in firefox.

Is there a better way, assuming I can't link directly to the PDF?

*****Get your SharpSide Swag!******
http://www.cafepress.com/sharpside/
*************** *************** *****


Nov 19 '05 #4
Matt Berther wrote:
Hello Scott,

You will want to make sure that you clear everything before you write
out.

[C#]
Response.ClearC ontent();
Response.ClearH eaders();
Response.Conten tType = "applicatio n/pdf";
Response.AddHea der("Content-disposition", "inline;filenam e=test.pdf");

Response.WriteF ile(file);
Response.Flush( );
Response.End();

I'm doing it exactly this way, and it works no matter where I run it
from (or which browser).


You should also set the Content-Length header, since some older Acrobat
Reader plug-ins require this.

Cheers,
--
http://www.joergjooss.de
mailto:ne****** **@joergjooss.d e
Nov 19 '05 #5
On Fri, 28 Jan 2005 13:17:10 -0800 in
microsoft.publi c.dotnet.framew ork.aspnet, Matt Berther
<mb******@hotma il.com> wrote:
[C#]
Response.Clear Content();
Response.Clear Headers();
Response.Conte ntType = "applicatio n/pdf";
Response.AddHe ader("Content-disposition", "inline;filenam e=test.pdf");

Response.Write File(file);
Response.Flush ();
Response.End() ;


Hi Matt,

Do you know if this approach works for other content-types as well?
Such as jpegs (image/jpeg) and Excel files (application/excel) etc.

I need to write a 'fileservice' page that will (based on a querystring
parameter) fetch a file from the server and blast it back to the
client. The documents accessed could be any file type imaginable. I
was planning on writing HttpHandlers for the various content types,
but Response.WriteF ile sounds like the silver bullet I was looking
for. Any thoughts?

Thanks,
Roger

Nov 19 '05 #6
Hello Roger,

It does. Of course, you'll have to change the ContentType.

I have done exactly what you describe in several solutions, however instead
of WriteFile, I use BinaryWrite to write out a byte array. If you have them
on the filesystem already, using WriteFile will work just fine.

A lot of times, Ill store images in the database and stuff them into the
cache on first load.

--
Matt Berther
http://www.mattberther.com
On Fri, 28 Jan 2005 13:17:10 -0800 in
microsoft.publi c.dotnet.framew ork.aspnet, Matt Berther
<mb******@hotma il.com> wrote:
[C#]
Response.ClearC ontent();
Response.ClearH eaders();
Response.Conten tType = "applicatio n/pdf";
Response.AddHea der("Content-disposition",
"inline;filenam e=test.pdf");
Response.WriteF ile(file);
Response.Flush( );
Response.End();

Hi Matt,

Do you know if this approach works for other content-types as well?
Such as jpegs (image/jpeg) and Excel files (application/excel) etc.

I need to write a 'fileservice' page that will (based on a querystring
parameter) fetch a file from the server and blast it back to the
client. The documents accessed could be any file type imaginable. I
was planning on writing HttpHandlers for the various content types,
but Response.WriteF ile sounds like the silver bullet I was looking
for. Any thoughts?

Thanks,
Roger


Nov 19 '05 #7
Matt Berther wrote:
Hello Roger,

It does. Of course, you'll have to change the ContentType.

I have done exactly what you describe in several solutions, however
instead of WriteFile, I use BinaryWrite to write out a byte array. If
you have them on the filesystem already, using WriteFile will work just
fine.

A lot of times, Ill store images in the database and stuff them into the
cache on first load.

--
Matt Berther
http://www.mattberther.com
On Fri, 28 Jan 2005 13:17:10 -0800 in
microsoft.publi c.dotnet.framew ork.aspnet, Matt Berther
<mb******@hotma il.com> wrote:
[C#]
Response.ClearC ontent();
Response.ClearH eaders();
Response.Conten tType = "applicatio n/pdf";
Response.AddHea der("Content-disposition",
"inline;filenam e=test.pdf");
Response.WriteF ile(file);
Response.Flush( );
Response.End();


Hi Matt,

Do you know if this approach works for other content-types as well?
Such as jpegs (image/jpeg) and Excel files (application/excel) etc.

I need to write a 'fileservice' page that will (based on a querystring
parameter) fetch a file from the server and blast it back to the
client. The documents accessed could be any file type imaginable. I
was planning on writing HttpHandlers for the various content types,
but Response.WriteF ile sounds like the silver bullet I was looking
for. Any thoughts?

Thanks,
Roger


Okay...I wrestled this all weekend, trying both WriteFile and
BinaryWrite. I couldn't get it to work. It turns out, in IE, if you
are using the full version of Acrobat then it hoses. But if you are
using the reader as the browser plugin then it's fine. So thanks for
the help. I was going crazy on this because it's such simple code.
Just something for the rest of you to be mindful of - Acrobat = bad!

--

_______________ _______________ ______________
Scott C. Reynolds - Tales From the SharpSide
http://www.scottcreynolds.com
sc***@scottcrey nolds.com

*****Get your SharpSide Swag!******
http://www.cafepress.com/sharpside/
*************** *************** *****
Nov 19 '05 #8
Hello Scott,

I do know that Adobe is rather buggy, and one thing that I found helped me
was to make the url end in .pdf

For example: the link that calls this code should look something like:

http://localhost/MyTestApp/GetFile.a...ame=report.pdf

For some reason, adding the extension there helps IE and Adobe out.

--
Matt Berther
http://www.mattberther.com
Matt Berther wrote:
Hello Roger,

It does. Of course, you'll have to change the ContentType.

I have done exactly what you describe in several solutions, however
instead of WriteFile, I use BinaryWrite to write out a byte array. If
you have them on the filesystem already, using WriteFile will work
just fine.

A lot of times, Ill store images in the database and stuff them into
the cache on first load.

--
Matt Berther
http://www.mattberther.com
On Fri, 28 Jan 2005 13:17:10 -0800 in
microsoft.publi c.dotnet.framew ork.aspnet, Matt Berther
<mb******@hotma il.com> wrote:

[C#]
Response.ClearC ontent();
Response.ClearH eaders();
Response.Conten tType = "applicatio n/pdf";
Response.AddHea der("Content-disposition",
"inline;filenam e=test.pdf");
Response.WriteF ile(file);
Response.Flush( );
Response.End();
Hi Matt,

Do you know if this approach works for other content-types as well?
Such as jpegs (image/jpeg) and Excel files (application/excel) etc.

I need to write a 'fileservice' page that will (based on a
querystring parameter) fetch a file from the server and blast it
back to the client. The documents accessed could be any file type
imaginable. I was planning on writing HttpHandlers for the various
content types, but Response.WriteF ile sounds like the silver bullet
I was looking for. Any thoughts?

Thanks,
Roger

Okay...I wrestled this all weekend, trying both WriteFile and
BinaryWrite. I couldn't get it to work. It turns out, in IE, if you
are using the full version of Acrobat then it hoses. But if you are
using the reader as the browser plugin then it's fine. So thanks for
the help. I was going crazy on this because it's such simple code.
Just something for the rest of you to be mindful of - Acrobat = bad!

*****Get your SharpSide Swag!******
http://www.cafepress.com/sharpside/
*************** *************** *****


Nov 19 '05 #9

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

Similar topics

2
3989
by: Jerry J | last post by:
I am using Response.WriteFile to send file streams back to web clients. My question is this: Can I use Response.WriteFile(SomeFilePath) for any size file? Will it handle chunking the data back to the client or do I have to do it myself?
2
5792
by: Carter | last post by:
i have a treeview on a webform (.aspx). when the user selects an appropriate node on the tvw. and clicks on a link button, i'm downloading a corresponding file to the client (from the server). so far so good. all of that works. after the download, i want to refresh the treeview and redisplay the current node with a different color. and also repopulate a property page on the right of the treeview but my problem is that during the file...
0
2034
by: ProJee | last post by:
Hi, Response.WriteFile (or Response.OutputStream.Write) finishes immediately, not after the file is completely downloaded. It finishes before (!) the user clicks the "Save" or "Open" browser button, file size doesn't matter. I'm not possible to track if the file was completely downloaded or if the user only clicked the link and cancelled the download. I've created a simple "solution" of this problem, which waits until the
2
4179
by: David Union | last post by:
Hi. I'm posting this here because I don't know exactly what the best group is. This is for an aspx page with Visual Basic as the code-behind page. I am doing very simple code... in the middle of an http request, i set a filename (with path) and do a Response.WriteFile(filenamewithpath) then Response.End(). I have tried Response.Clear() and .Flush() ahead of time.
1
1168
by: oz | last post by:
private void Page_Load(object sender, System.EventArgs e) { if(!IsPostBack) { String FileName; FileInfo MyFileInfo; Long StartPos = 0, FileSize; FileName = "J:\\Summary.txt"; MyFileInfo = new FileInfo(FileName); FileSize = MyFileInfo.Length;
4
2924
by: david | last post by:
I has a question: I can use Response.WriteFile to display images such as .jpg. But I can not us it to display words doc file in EI by calling Response.WriteFile("testdoc/DownloadLarge.doc"). It only shows unreadable chars. How to use it to show the doc file on webrowser? Thanks
1
3918
by: Ryan Pedersen | last post by:
I have been trying to figure out how to transmit a file back to a user using the response.transmitfile or response.writefile method and just not having much success. I have a dell server running windows 2003 web server edition fully patched. The files are physically on the same web server. The entire site is HTTPS... it has to be because it is a financial site. With SSL on I cannot get TransmitFile or WriteFile to work (all files are less...
3
6373
by: Buddy Ackerman | last post by:
I'm trying to write files directly to the client so that it forces the client to open the Save As dialog box rather than display the file. On some occasions the files are very large (100MB+). On these files teh time that it takes until the client displays the Save As dialog can be extrordinarily long (3+ minutes). I don't understand why. I was initiall using the format: Respnse.writefile("filepath", offset, length) but that simply...
3
10426
by: MJP | last post by:
I have a button which kicks off the generation of a report after which the file will be downloaded. The report generation can take a long time, so client side onclick event of the button also makes a <span> tag visible which contains a nice message to the user thanking them for their patience. Of course after the report has been generated and downloaded this message should be removed. However, the RegisterStartupScript isn't working...
0
10223
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10051
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.