Solved

Save PDF from asp and get asp code ?

Posted on 2010-11-11
6
232 Views
Last Modified: 2012-05-10
Hi all,
I have an app that simply shows a PDF file.
When pressing the "save" icon it defualts to "default" as the file name and saves it as pure asp code (jibberish).  If I change the name of the file to anything it saves as a proper PDF file.
What am I doing wrong?
0
Comment
Question by:Thunder724
  • 4
  • 2
6 Comments
 
LVL 3

Expert Comment

by:mortimer452
ID: 34117274
How are you displaying the PDF file?

Sounds like you aren't setting the Content-Type and Content-Disposition headers correctly.  Should be something like this:

Response.ClearHeaders()
Response.AddHeader("Content-Type","Application/octet-stream")
Response.AddHeader("Content-Disposition","attachment; filename=MyFile.pdf")
0
 
LVL 4

Author Comment

by:Thunder724
ID: 34117315
thanks mortimer...
i'm an acomplished client server developer; but web dev is very new to me.  I try to code pure vb.net and and confused to the asp page code part of everything... below is my vb code..

    Public Sub ViewFile(ByVal FName$)
        Dim path As String = FName$
        Dim file As New System.IO.FileInfo(path)

        If file.Exists Then
            Response.Clear()

            Select Case UCase(file.Extension)
                Case ".PDF"
                    Response.ContentType = "Application/pdf"
                Case ".DOC", ".DOCX"
                    Response.ContentType = "Application/msword"
            End Select

            Response.WriteFile(file.FullName)
            Response.End()

        End If
    End Sub

    Protected Sub btnPatronInfo_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPatronInfo.Click
        Dim OpenFile$ = Server.MapPath(".") & "\Forms\BnaiPatronInfoWeb1.pdf"
        ViewFile(OpenFile$)
    End Sub

    Protected Sub btnAuctionitems0_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOrderTickets.Click
        Dim OpenFile$ = Server.MapPath(".") & "\Forms\Auction2010TicketFormWeb1.pdf"
        ViewFile(OpenFile$)
    End Sub

    Protected Sub btnViewInventation_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnViewInventation.Click
        Dim OpenFile$ = Server.MapPath(".") & "\Forms\Auction2010InviteWeb2.pdf"
        ViewFile(OpenFile$)
    End Sub
0
 
LVL 3

Accepted Solution

by:
mortimer452 earned 500 total points
ID: 34118167
Yeah your problem is that even though you're telling the browser the content-type is a PDF, the page name is still .aspx so teh end-user has to rename it after saving

Use the code I specified above, specifying "application/octet-stream" as the content-type.  This tells the browser it's just generic binary data

The content-dispostion of "attachment; filename=MyFile.pdf" forces the browser to prompt with the Open/Save dialog allowing the user to either view the file in their browser or save it to disk, and provides a filename.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Author Comment

by:Thunder724
ID: 34119781
I'm getting the following error when I either open or save the file and open in adobe?
0
 
LVL 4

Author Comment

by:Thunder724
ID: 34119786
Sorry, here it is again.. error message
0
 
LVL 4

Author Comment

by:Thunder724
ID: 34121791
Hey I got it working thanks :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now