Streaming files to Firefox

W

WJ

I have some simple code that reads a file from disk and streams it to a web
browser.

The servlet container is Tomcat 5.0.28
IE 6.0.3790
Firefox 1.0.3

This works for IE, but not Firefox:

ServletOutputStream out = res.getOutputStream();
File testFile = new File("C:/temp/MyTestPDF.pdf");

res.setHeader("Content-Type","application/pdf");
res.setHeader("Content-Length","4230364");
res.setHeader("Content-Disposition", "inline;
filename=\"MyTestPDF.pdf");

int bufferSize;
byte[] buffer=new byte[2048];
FileInputStream fis=new FileInputStream(testFile);
while( (bufferSize=fis.read(buffer)) != -1)
out.write(buffer, 0, bufferSize);
out.close();


Now, if I change the Content-Disposition to this:
res.setHeader("Content-Disposition", "attachment;
filename=\"MyTestPDF.pdf");
Both browsers correctly prompt me to open or download the file.

I want the file to stream (eventually into a new window) without being
prompted.
Any help is greatly appreciated!
 
A

Adam P. Jenkins

WJ said:
I have some simple code that reads a file from disk and streams it to a web
browser.

The servlet container is Tomcat 5.0.28
IE 6.0.3790
Firefox 1.0.3

This works for IE, but not Firefox:

ServletOutputStream out = res.getOutputStream();
File testFile = new File("C:/temp/MyTestPDF.pdf");

res.setHeader("Content-Type","application/pdf");
res.setHeader("Content-Length","4230364");
res.setHeader("Content-Disposition", "inline;
filename=\"MyTestPDF.pdf");

int bufferSize;
byte[] buffer=new byte[2048];
FileInputStream fis=new FileInputStream(testFile);
while( (bufferSize=fis.read(buffer)) != -1)
out.write(buffer, 0, bufferSize);
out.close();


Now, if I change the Content-Disposition to this:
res.setHeader("Content-Disposition", "attachment;
filename=\"MyTestPDF.pdf");
Both browsers correctly prompt me to open or download the file.

I want the file to stream (eventually into a new window) without being
prompted.
Any help is greatly appreciated!

You didn't mention what error you get from Firefox. Probably you just
need to enable the Acrobat Reader plugin under Firefox to make inline
PDFs work.
 
S

shakah

I'd expect the behavior to be client-dependent based on the browser
configuration. But it does look like you have an unbalanced
double-quote in your "Content-Disposition:" headers -- shouldn't the
value be "inline; filename=\"MyTestPDF.pdf\"" ?
 
W

WJ

Sorry. :)

The page just hangs when I try the download inline. If I do it as an
attachment,
it works fine in Firefox.

I thought of the plugin issue. Under tools -> Options ->Downloads -> I
have.pdf
associated with AcroExch, and under the Plug-ins button, it is enabled.
 
W

WJ

When I say the page hangs, I see the progress bar go across the bottom,
and on the Tomcat console, I see the pdf streaming down. It's as if
Firefox gets it, then does nothing with it. Afterward, Firefox is not
frozen,
but doesn't respond when I resize it, although the buttons work.
 
W

WJ

ok. I figured this out and am a bit surprised.

On my main dev box, I am running Windows 2003 Server.
This is where Firefox fails inline.

I went to another machine running Windows 2000 and
it ran just fine! So it seems to be an issue with Windows
Server 2003!

Thanks for all your help.
 
A

Abrasive Sponge

WJ said:
I have some simple code that reads a file from disk and streams it to a web
browser.

The servlet container is Tomcat 5.0.28
IE 6.0.3790
Firefox 1.0.3

This works for IE, but not Firefox:

ServletOutputStream out = res.getOutputStream();
File testFile = new File("C:/temp/MyTestPDF.pdf");

res.setHeader("Content-Type","application/pdf");
res.setHeader("Content-Length","4230364");
res.setHeader("Content-Disposition", "inline;
filename=\"MyTestPDF.pdf");

int bufferSize;
byte[] buffer=new byte[2048];
FileInputStream fis=new FileInputStream(testFile);
while( (bufferSize=fis.read(buffer)) != -1)
out.write(buffer, 0, bufferSize);
out.close();


Now, if I change the Content-Disposition to this:
res.setHeader("Content-Disposition", "attachment;
filename=\"MyTestPDF.pdf");
Both browsers correctly prompt me to open or download the file.

I want the file to stream (eventually into a new window) without being
prompted.
Any help is greatly appreciated!
Well, my suggestions are

1. why don't you use...file.length() to get the file length correctly
2. why don't you use...response.setContentLength()?
3. why don't you use...response.setContentType()?
4. why don't you use...BufferedOutputStream to do your buffering work?
5. why don't you use the correct try..catch...finally structure with
your servlet?
 
C

Chris Smith

WJ said:
On my main dev box, I am running Windows 2003 Server.
This is where Firefox fails inline.

I went to another machine running Windows 2000 and
it ran just fine! So it seems to be an issue with Windows
Server 2003!

Unless you've tried with several substantially different installations
of Windows Server 2003, it's more likely a configuration issue on your
specific system.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

Adam P. Jenkins

Actually, this may be your problem, if this is your actual code. You
have a hard-coded content-length. If your PDF file is actually shorter
than this, then Firefox may be waiting to receive the rest of it,
whereas maybe IE just handles this error more gracefully. I'd change
those lines to:

res.setContentType("application/pdf");
res.setContentLength((int)file.length());
 
W

WJ

The real problem I was having was streaming a PDF from jasper reports.

In my code, I am using res.setHeader("Content-Type","application/pdf");

When I couldn't get that to work, I wrote a scratch servlet to stream a pdf
from my own file system, setting the length as
res.setContentLength((int)testFile.length());

That didn't work either, so I went back to basics, hard coding value I knew.

I've wanted to download the jasper source to see if it actually sets the
contentLength.
I never did because it's worked great thus far, until I ran into this, which
probably
isn't a jasper issue anyway.

Thanks for all the feedback!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top