I have the following code which successfully writes a pdf to the client. The problem is that I can't get any code after this to execute. It's the last step of a wizard, and despite putting this in the ActiveStepChanged handler, it never makes it to the confirmation/final page.
Response.Clear()
Response.ContentType = Nothing
Response.AddHeader("content-disposition", "attachment; filename=" & FileName)
Response.BinaryWrite(data)
Response.Flush()
Basically, there's a checkbox that the user checks if they want to download the file when they hit the Finish button. I don't want to have a separate button to download the file because users have been known to get confused and think that by pressing the download button that they've completed the necessary steps and never complete their application (we're talking about non computer literate users here). So it all works, except it doesn't make it to the confirmation step when they select that option.
How can I ensure that processing continues after downloading the file?
The best way is to write this code in a HttpHandler and call Response.End after Response.Flush.
I figured this one out myself. Basically, I placed the response.binarywrite code (listed in op) in the code behind of its own empty webform. I then call then use the javascript openwindow function in ScriptManager.RegisterClientScriptBlock to open it.
Code in original page:
Me.Session("PrintApplication") = data
Me.Session("PrintApplicationFileName") = FileName
ScriptManager.RegisterClientScriptBlock(Me, Me.GetType, "PrintReport", "window.open('PrintApplication.aspx');", True)
Code in PrintApplication.aspx:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Me.Session("PrintApplicationFileName") <> Nothing Then
Dim data As Byte() = Me.Session("PrintApplication")
Dim FileName As String = Me.Session("PrintApplicationFileName")
Me.Session("PrintApplication") = Nothing
Me.Session("PrintApplicationFileName") = Nothing
Response.Clear()
Response.ContentType = Nothing
Response.AddHeader("content-disposition", "attachment; filename=" & FileName)
Response.BinaryWrite(data)
Response.Flush()
Response.End()
End If
End Sub
Works perfectly
Related
I'm trying to generate a PDF using Winnovative Tools. In my click handler which fires after clicking on the "Print PDF button", I have the code below.
I expect control to resume to the printPDF_Click sub routine after calling Server.Execute() but instead it calls the printPDF_Click sub routine from scratch which causes a loop because Server.Execute() will be called again and so on.
It works as expected when I set preserveForm as False but then I lose my form data and the point is retaining it.
Private Sub printPDF_Click(sender As Object, e As EventArgs) Handles printPDF.Click
Dim outTextWriter As IO.TextWriter = New IO.StringWriter()
Server.Execute("Default_v3.aspx?isWinnovative=true", outTextWriter)
Dim baseUrl As String = HttpContext.Current.Request.Url.AbsoluteUri
Dim htmlStringToConvert As String = outTextWriter.ToString()
Dim downloadBytes As Byte() = PdfHelper.CreatePdf(htmlStringToConvert, baseUrl)
Dim response As HttpResponse = HttpContext.Current.Response
response.Clear()
response.AddHeader("Content-Type", "binary/octet-stream")
response.AddHeader("Content-Disposition", ("attachment; filename=" + (
"Rendered.pdf" + ("; size=" + downloadBytes.Length.ToString))))
response.Flush()
response.BinaryWrite(downloadBytes)
response.Flush()
response.End()
End Sub
Chrome has a setting that blocks the opening of local files. The specific error you get is Not allowed to load local resource: file:///<file name>. I'm trying to develop an internal site for us to access these files but as many users are using Chrome, I need a workaround to serve these files.
Currently, to get my files and display them, I have this logic:
For Each file As String In Directory.GetFiles("<file path>")
fileInfo = New FileInfo(file)
fileList.Add(fileInfo)
Next
fileList.Sort(Function(x, y) x.Name.CompareTo(y.Name))
For i As Integer = 0 To fileList.Count - 1
pnlLinks.Controls.Add(New LiteralControl("<br />"))
pnlLinks.Controls.Add(New LiteralControl("" & fileList(i).Name & ""))
pnlLinks.Controls.Add(New LiteralControl("          "))
pnlLinks.Controls.Add(New LiteralControl("<span>" & GetByteSize(fileList(i).Length.ToString) & " </span>"))
pnlLinks.Controls.Add(New LiteralControl("<br />"))
Next
The a href is a little funky because I've been experimenting with different folder structures to get this working, but it should not be relevant.
I had the idea of implementing a function that could send this to a byte stream but I'm not experienced in that area and wouldn't be sure how to implement it.
I was able to resolve this with a FileStream and launching to a new page that handles it on Page_Load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim pdfFileStream As FileStream = Nothing
Dim pdfFileSize As Long = 0
Dim pdfPath As String = "<file path>"
pdfFileStream = New FileStream(pdfPath, FileMode.Open)
pdfFileSize = pdfFileStream.Length
Dim Buffer(CInt(pdfFileSize)) As Byte
pdfFileStream.Read(Buffer, 0, CInt(pdfFileSize))
pdfFileStream.Close()
Response.ContentType = "application/pdf"
Response.OutputStream.Write(Buffer, 0, pdfFileSize)
Response.Flush()
Response.Close()
End Sub
I'm trying to restrict anonymous users from browsing directly to a particular filename (image file) in a folder on my website. But when I turn on the "IIS Authentication" feature on the folder, both anonymous users and the website application can't access the image file.
How can I deny access to the file for anonymous users (for example, if the user were to type in the absolute url), but allow access to the website application? (I thought that maybe the "IP Address and Domain Restrictions" feature could be used, as well, but couldn't get it to work)
I could move the image file to a folder outside of the website, but then not sure how to use it in the .ImageUrl property.
...bump
EDIT - Solution (put following in a .aspx page and set the ImageUrl property to this page, with any needed querystring parameters):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Request.QueryString("FileType") IsNot Nothing) And (Request.QueryString("FileName") IsNot Nothing) Then
Try
' Read the file and convert it to Byte Array
Dim filePath As String = UrlXlat(Request.QueryString("FileType") & "\")
Dim fileName As String = Request.QueryString("FileName")
Dim contentType As String = "image/" & Path.GetExtension(fileName).Replace(".", "")
Dim fs As FileStream = New FileStream(filePath & fileName, FileMode.Open, FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(Convert.ToInt32(fs.Length))
br.Close()
fs.Close()
'Write the file to Reponse
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = contentType
Response.AddHeader("content-disposition", "attachment;filename=" & fileName)
Response.BinaryWrite(bytes)
Response.Flush()
Response.End()
Catch ex As Exception
response.write(ex):response.end
End Try
End If
End Sub
You can move the file either to a folder outside of the root of the site or to the App_Data folder (which is protected form direct browsing by the ASP.NET framework) and then set your ImageUrl to point to a generic handler (.ashx file) which will be responsible for delivering the file to the browser. You perform your authentication checks in the handler.
I've written an article that provides implementation details: http://www.mikesdotnetting.com/article/122/simple-file-download-protection-with-asp-net
I'm currently trying to open a PDF file on my website that is located on my company's network. I had this working previously, but now for some reason it isn't working. Here is what I have:
I am using impersonation to access the file. It has domain admin privileges. This is from my web.config file (username/password are altered):
<identity impersonate="true" password="pass" userName="domain\user" />
I use this code to open the PDF in a window:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim strPath As String = CStr(Session("DWGPath"))
strPath = "file://san01/prodeng/" + Mid(strPath, 4)
strPath = Replace(strPath, "\", "/")
Dim pdfPath As String = strPath
Dim client As WebClient = New WebClient()
Dim buffer As Byte() = client.DownloadData(pdfPath)
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", buffer.Length.ToString())
Response.BinaryWrite(buffer)
Response.End()
Catch exa As WebException
Response.Redirect("DrawingError.aspx")
Catch ex As Exception
Throw ex
End Try
End Sub
This doesn't work. It redirects me to the "DrawingError.aspx" page. This link dispalys the "Session("DWGPath")" variable. I can take this variable and paste it in to my browser and the PDF opens without problem.
However, if I alter my code to this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim strPath As String = CStr(Session("DWGPath"))
Dim pdfPath As String = strPath
Dim client As WebClient = New WebClient()
Dim buffer As Byte() = client.DownloadData(pdfPath)
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", buffer.Length.ToString())
Response.BinaryWrite(buffer)
Response.End()
Catch exa As WebException
Response.Redirect("DrawingError.aspx")
Catch ex As Exception
Throw ex
End Try
End Sub
It still doesn't work.
The account also has full control privileges to the folder that contains the PDFs.
Any help or insight would be appreciated. Thank you!
EDIT: IF I throw exa then I get this:
The account used is a computer account. Use your global user account or local user account to access this server.
I assume you're running IIS. Go to the application pool for this app and change the identity that it's running under to be the domain\user account. See if that fixes your problem.
You want to make sure that the password on this account doesn't change or else it will fail when the password expires.
I am trying to display a dynamically created PDF (Byte()) loading the default viewer int he client browser. The following code works with all browsers except the microsoft ones consistently. IE9 Wants to run MSXML 3.0 SP11 in response to the page. Here is the code generating the page:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim pdfData() As Byte
'load PDF Data
If pdfData.Length < 1 Then
Response.Write("ERROR - No data")
Response.End()
Return
End If
Response.Clear()
Response.AddHeader("Content-Disposition", "inline;filename=newcomp.PDF")
Response.AddHeader("Content-Length", pdfData.Length.ToString())
Response.AddHeader("Pragma", "no-cache")
Response.ContentType ="application/pdf"
Response.BinaryWrite(pdfData)
Response.Flush()
HttpContext.Current.ApplicationInstance.CompleteRequest()
End Sub
I call the page like this: http://localhost/DisplayPdf.aspx?newcomp.pdf
You can try it yourself with this link: http://www.netvaluecentral.com/DisplayPdf.aspx?newcomp.pdf
Any ideas?