Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PDF Reports

Status
Not open for further replies.

JasonNevin

Programmer
May 26, 2005
29
GB
I have a VB.NET web form that creates a PDF report based on some database results. The form uses response.redirect to display the file it creates. What this means of course is that the user has to use the back button in their browser to return to the page from where they triggered the report request. I would prefer if possible to spawn the report web form into a new window so that the user can just close it or keep it in the background as is required. Is there anyway of doing this in vb.net? Does anyone know of a better way of doing this?

Thanks
 
Yep...try something like:
Code:
    Private Sub DownloadFile(ByVal virtualPath As String)
        ' retrieve the physical path of the file to download, and create
        ' a FileInfo object to read its properties
        Dim FilePath As String = Server.MapPath(virtualPath)
        Dim TargetFile As New System.IO.FileInfo(FilePath)
        ' clear the current output content from the buffer
        Response.Clear()
        Response.CacheControl = "Private"
        ' add the header that specifies the default filename for the Download/
        ' SaveAs dialog
        Response.AddHeader("Content-Disposition", "attachment; filename=" + _
            TargetFile.Name)
        ' add the header that specifies the file size, so that the browser
        ' can show the download progress
        Response.AddHeader("Content-Length", TargetFile.Length.ToString())
        ' specify that the response is a stream that cannot be read by the
        ' client and must be downloaded
        Response.ContentType = "application/octet-stream"
        ' send the file stream to the client
        Response.WriteFile(TargetFile.FullName)
        ' stop the execution of this page
        Response.End()
    End Sub
As you've set the ContentType to "octet-stream", in most cases it should ask the user if they want to open or save the file. You may also want to add to that function to make surte the file actually exists.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Excellent. Opened a new window as I required. Only problem was that the redirect I performed after running DownloadFile is not being executed so my users still have to use the back button. Is my method correct?

code:

DownloadFile(strPDFFile)

Response.Redirect("Details.aspx")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top