Solved

Response.ContentType = "application/pdf"

Posted on 2004-08-21
16
119,515 Views
Last Modified: 2011-08-18
I am trying to display a PDF file (which I am being passed from a web service as a binary stream) in a browser, but I am being prompted to save the file instead.  I don't want the user to be prompted; I just want the PDF to be displayed.  Here is the code:

            Dim binaryData() As Byte

            binarydata = GetDataHere()
            Response.ContentType = "application/pdf"
            Response.BinaryWrite(binaryData)
            Response.End()
           
I also tried adding Response.Flush() and Response.Close() just for good measure, but it didn't help.

I'm using ASP.NET.  Any ideas?
0
Comment
Question by:KingJosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
16 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 11862409
Hi

It could be because of the Response.ContentType = "application/pdf". Try commenting that out or changing it to text/html.

Regards,
Zyloch
0
 

Author Comment

by:KingJosh
ID: 11862473
I tried both commenting out that line and replacing it but both times I was prompted to save the file.  Does it make a difference that GetDataHere returns a regular STRING, which I then convert (using System.Convert.FromBase64String)?  When I write out the converted bytes to a file then response.redirect to that file it works fine...I just don't want to have to write out the file every time...
0
 
LVL 20

Expert Comment

by:ihenry
ID: 11871154
try this,

Dim binaryData() As Byte
binarydata = GetDataHere()
Response.ContentType = "application/pdf"
Response.OutputStream.Write(binaryData, 0, FileSize)
Response.BinaryWrite(binaryData)
Response.End()


HTH
Henry
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:KingJosh
ID: 11872144
Same results...I added the code:
            Response.ContentType = "application/pdf"
            Response.OutputStream.Write(binaryData, 0, binaryData.Length)
            Response.BinaryWrite(binaryData)
            Response.End()

(I used binarydata.length for the file size)

First it choked on the Response.End ("Thread was being aborted."), but now it's just prompting me to save the results...
0
 
LVL 20

Expert Comment

by:ihenry
ID: 11872202
oops.. I forgot something..

remove this line,
Response.BinaryWrite(binaryData)

it should works, I have tested it.
0
 

Author Comment

by:KingJosh
ID: 11872312
Sorry...Still getting the prompt.  It's weird...When I take that binaryData and convert it into a regular string, then create a file on the server, I can response.redirect to it with no problem...
0
 
LVL 20

Accepted Solution

by:
ihenry earned 250 total points
ID: 11872421
I simulate the binary data using this code,

Dim MyFileStream As FileStream
Dim FileSize As Long

MyFileStream = New FileStream(filePath, FileMode.Open)
FileSize = MyFileStream.Length

Dim Buffer(CInt(FileSize)) As Byte
MyFileStream.Read(Buffer, 0, CInt(FileSize))
MyFileStream.Close()

Response.ContentType = "application/pdf"
Response.OutputStream.Write(Buffer, 0, FileSize)
Response.Flush()
Response.Close()

And this is working find in my machine.
0
 
LVL 20

Expert Comment

by:ihenry
ID: 11872457
if you are still having the same problem after using the code from my previous comment, can you post more code here?
0
 

Author Comment

by:KingJosh
ID: 11872778
I don't understand what the difference is, but it works now!  Thanks - J
0
 

Author Comment

by:KingJosh
ID: 11891826
I'm having a new problem with this...If anyone wants a quick 250 points, see question Q_21106095.html.
0
 

Expert Comment

by:ESH
ID: 11897452
Dear? Sir:
You post to refer an question (Q_21106095.html.) which exists no anymore.  Prehaps, made deleted??!@?

As I am,
Vikläs
0
 

Author Comment

by:KingJosh
ID: 11902137
That is correct, ESH.  I had problems posting the question (it wasn't showing up in the searches, etc." so I deleted it.  I think there's an issue with the length of the question being posted...Thank you anyway for your interest.
0
 

Expert Comment

by:madpuppy
ID: 11947649


I really need to do that same thing in COLD FUSION CFM if you have any help for me....

http://www.experts-exchange.com/Web/WebDevSoftware/ColdFusion/Q_21113411.html
0
 

Author Comment

by:KingJosh
ID: 11948531
Sorry...I never used Cold Fusion...
0
 

Expert Comment

by:papaflo
ID: 12195851
how do you do this in the old ASP or in VB 6?
0
 

Author Comment

by:KingJosh
ID: 12286005
The accepted answer should work for ASP as well.  
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question