Solved

Save PDF from asp and get asp code ?

Posted on 2010-11-11
6
231 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Author Comment

by:Thunder724
Comment Utility
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
Comment Utility
Sorry, here it is again.. error message
0
 
LVL 4

Author Comment

by:Thunder724
Comment Utility
Hey I got it working thanks :)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 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

9 Experts available now in Live!

Get 1:1 Help Now