locked
Stream pdf file to new browser window using response.writefile RRS feed

  • Question

  • User1456474806 posted
    Help need some help with VB.NET code problem (stream pdf file to new browser window) I have spent many unsuccesfull hours attempting to have a button link on my datagrid to cause a PDF file to be streamed to a new instance of a browser window. I can launch a new browser window when I force a file download, which I do not want to do. These small PDF forms contain sensitive data so I don't want them sitting on users hard drives by default. The "response.writefile" (inline) methods works best for me becuase it streams the file to the browser for immeadiate viewing and does not expose a URL path to the file (would be nice if I could disable the Adobe save file button). The probem with using my current datagrid aspx window for the PDF view is: that after reviewing the PDF file, the user needs to continue working with other items in the datagrid which required they hit the back button (they usually end up closing it!). The back button takes them back to the page prior to the datagrids. for this reason I need to keep my datagrid.aspx page alive and have the PDF streamed to a new browser window instance. I have experimented with server.execute, and response.write (javasript window.open) however new windows are not launched until a response.end or endsub is reached, and it allears that I can only use the response.write/writefile once. Once I have done the first response.write( javascript) to open the second window, I get an error when performing the following response.writefile to stream the pfd form - "System.Web.HttpException: Server cannot clear or set headers after HTTP headers have been sent" Is there a simple way to specify that the response.writefile should be done in a new child window? Sorry for the verbose problem desciption. Any help is greatly apreciated Code: My ASCX.VB file: Private Sub DG_Roster_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG_Roster.ItemCommand 'when user selected row linkbutton ' open new window using the same ural as this one strWin.Append("<script language='javascript'>") strWin.Append("window.open('" & myURL & "')</") 'uses url of the current window to launch a new copy of it strWin.Append("script" & ">") Response.Write(strWin.ToString()) ' Get Configured directory containing secured forms from webconfig. Dim SecuredFormPath As String = System.Configuration.ConfigurationSettings.AppSettings("securedformpath") Dim PDFFormFilePath = SecuredFormPath & FormDwnldName Dim FormPhyiscalFilePath As String = Server.MapPath(PDFFormFilePath) Dim dwnldForms As New ASPNetPortal.FileHandling() ' '' call my general purpose files handler" Params: (1) filepath, (2)run in browser/force dwnld, (3)file type dwnldForms.DownloadFile(FormPhyiscalFilePath, False, myURL, "application/pdf") end sub ========================================== Public Class FileHandling ' FileHandler.vb Public Shared Sub DownloadFile(ByVal FilePath As String, ByVal forceDownload As Boolean, ByVal myURL As String, Optional ByVal ContentType As String = "") If File.Exists(FilePath) Then Dim myFileInfo As FileInfo Dim StartPos As Long = 0, FileSize As Long, EndPos As Long myFileInfo = New FileInfo(FilePath) FileSize = myFileInfo.Length EndPos = FileSize HttpContext.Current.Response.Clear() HttpContext.Current.Response.ClearHeaders() HttpContext.Current.Response.ClearContent() Dim path As path Dim fullpath = path.GetFullPath(FilePath) Dim name = path.GetFileName(fullpath) Dim ext = path.GetExtension(fullpath) Dim type As String = ContentType If Not IsDBNull(ext) Then ext = LCase(ext) End If If type = "" Then Select Case ext Case ".htm", ".html" type = "text/HTML" Case ".txt" type = "text/plain" Case ".doc", ".rtf" type = "Application/msword" Case ".csv", ".xls" type = "Application/x-msexcel" Case ".pdf" type = "Application/pdf" Case Else type = "text/plain" End Select End If If type <> "" Then HttpContext.Current.Response.ContentType = type End If Dim Range As String = HttpContext.Current.Request.Headers("Range") If Not ((Range Is Nothing) Or (Range = "")) Then Dim StartEnd As Array = Range.Substring(Range.LastIndexOf("=") + 1).Split("-") If Not StartEnd(0) = "" Then StartPos = CType(StartEnd(0), Long) End If If StartEnd.GetUpperBound(0) >= 1 And Not StartEnd(1) = "" Then EndPos = CType(StartEnd(1), Long) Else EndPos = FileSize - StartPos End If If EndPos > FileSize Then EndPos = FileSize - StartPos End If HttpContext.Current.Response.StatusCode = 206 HttpContext.Current.Response.StatusDescription = "Partial Content" HttpContext.Current.Response.AppendHeader("Content-Range", "bytes " & StartPos & "-" & EndPos & "/" & FileSize) End If HttpContext.Current.Response.AppendHeader("Content-disposition", "inline; target=" & "_blank") If Not (type = "") And (StartPos = 0) Then HttpContext.Current.Response.ContentType = type End If If (forceDownload) Then ' Will cause a download dialog (not using this method for the PDFs) HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment; filename=" & myFileInfo.Name) End If HttpContext.Current.Response.WriteFile(FilePath, StartPos, EndPos) HttpContext.Current.Response.Flush() End If End Sub End Class End Namespace
    Monday, January 20, 2003 8:16 PM

All replies

  • User-528039901 posted
    No way, that is not the way HTTP works. The file is streamed to the browser window that requested it. THAT SAID - the request is where you have to attack this. You basically have to start the request from a new window. For example by setting an appropriate "target" tag in the link cloicked, or by streaming javascript code there to open a new browser and load the URL for the PDF there.
    Tuesday, January 28, 2003 9:25 AM
  • User1831057809 posted
    yepp that's right. additionally: you don't want that the user to see the pdf file (or the url from which that loads). when you use sessionid's give the newly opened window this sessionid as parameter so only this is able to see this file also i would do a timestamp (encrypted) in the querystring so he could not give that url to a friend or somehow who is not allowed to view that page. after 1 the timestamp is invalid and the user cannot see that document. additionally the url of the pdf file you could encrypt with the sessionid and timestamp or if you generate it automatically you can encrypt the parameters for generating the document with the above two parameters.
    Sunday, February 9, 2003 9:34 AM
  • User754841323 posted
    If a am using a Web Form button (not HTML), what is the code for launching a new instance of browser window from where I can stream the PDF?
    Tuesday, January 6, 2004 1:39 AM