Downloading file from aspx page -- change file name?

  • Thread starter Thread starter Brandon Potter
  • Start date Start date
B

Brandon Potter

Simple question --

I have an ASPX page that accepts the ID of a document in the query string to
be downloaded; however, the file is not in the web application and has to be
read from another directory.

Furthermore, I need to be able to check some session variables to make sure
the user has permission to download that document and didn't just change the
querystring ID.

Since the file is being passed to the user via the Response stream is there
any way to change the name of the file that the browser downloads by default
to be something other than the name of the ASPX page?

Brandon
 
You should be able to give it a filename and fetch it from a UNC path like
this:

Dim strUNCFilePath As String =
ConfigurationSettings.AppSettings(Replace(Request.ServerVariables("SERVER_NAME"),
".", "_") & "_UNCFilePath")
strPDFname = strFilepath.Substring(strFilepath.LastIndexOf("/") + 1)
strFileExtension =
strFilepath.Substring(strFilepath.LastIndexOf(".") + 1)

If e.CommandName = "dnld" Then
' Calculate the filename by taking everything after the final
slash
strPDFname = strFilepath.Substring(strFilepath.LastIndexOf("/")
+ 1)
strFileExtension =
strFilepath.Substring(strFilepath.LastIndexOf(".") + 1)

' Set up the response headers for a PDF download
Response.Clear()
Response.ClearHeaders()
Response.ClearContent()

' Create a suggested filenname for the downloaded document
(short - not the whole path)
Response.AddHeader("Content-Disposition", "attachment;filename="
& strPDFname)

'Write the file to the browser and end the HTML content
Response.WriteFile(Server.MapPath(strFilepath))
Response.End()
End If
 
Back
Top