Allow download of filenames with &'s in them
I want to allow users to download files that have names containing & characters.
We've escaped our way through the problem but cannot seem to get around one last obstacle.
Here in Download.asp.vb we have
Code:
Imports System.IO
Partial Class Clients_Download
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ctx As HttpContext = HttpContext.Current
Dim response As HttpResponse = ctx.Response
Dim sFile As String = Request.QueryString("filename".ToString)
' Buffer to read 10K bytes in chunk:
Dim buffer(10000) As Byte
' Length of the file:
Dim length As Integer
' Total bytes to read:
Dim dataToRead As Long
' Identify the file to download including its path.
Dim filepath As String = sFile
' Identify the file name.
Dim filename As String = System.IO.Path.GetFileName(filepath)
Using iStream As New FileStream(filepath, System.IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
' Total bytes to read:
dataToRead = iStream.Length
response.ContentType = "application/octet-stream"
response.AddHeader("Content-Disposition", "attachment; filename=" & filename)
And here is where it fails - the attachment; filename= line.
The filename vbl has the & character within it.
The pop-up that appears in the browser window asking for where to save the file shows a filename WITHOUT the &'s.
Here is the continuation of the code
Code:
' Read the bytes.
While dataToRead > 0
' Verify that the client is connected.
If response.IsClientConnected Then
' Read the data in buffer
length = iStream.Read(buffer, 0, 10000)
' Write the data to the current output stream.
response.OutputStream.Write(buffer, 0, length)
' Flush the data to the HTML output.
response.Flush()
ReDim buffer(10000) ' Clear the buffer
dataToRead = dataToRead - length
Else
'prevent infinite loop if user disconnects
dataToRead = -1
End If
End While
iStream.Close()
'delete the file after downloading it
If Request.QueryString("delfile") = "true" Then
'make sure it contains \temp\ dir
If sFile.Contains("\temp\") Then
File.Delete(sFile)
End If
End If
response.End()
End Using
End Sub
2 Attachment(s)
Re: Allow download of filenames with &'s in them
It works ok for me.
I assume this is a typo,
Dim sFile As String = Request.QueryString("filename".ToString)
and should be changed to:
Dim sFile As String = Request.QueryString("filename").ToString
This is what I get, running your code as it is.
In the first box, filename is in a label, so it assumes & as shortcut and shows as underscore. But in the actual box where I specify filename to save, I can see the "&" in filename.
Attachment 78701
Attachment 78702
Re: Allow download of filenames with &'s in them
Hey,
You can try your luck with HttpServerUtility.HtmlEncode Method. This will convert characters to their respective HTML Hex format.
Re: Allow download of filenames with &'s in them
BTW how big are your files? Why do you need to read/write in chunks?
This code is a simple one-liner and has always worked for me:
vb.net Code:
Sub TransmitFile(ByVal fileName As String)
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; filename=""" & System.IO.Path.GetFileName(fileName) & """")
Try
Response.TransmitFile(fileName)
Catch ex As Exception
Response.Write(ex.ToString)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fileName As String = "C:\Temp\abc&d.txt"
TransmitFile(fileName)
End Sub
Re: Allow download of filenames with &'s in them
I'd suggest the URLEncode instead... otherwise you'll get& in the file name. By URLEncoding it, you get the %XX where XX is the ascii code (for instance a space comes out as %20). the browser & client should be able to sort it out from there.
-tg
Re: Allow download of filenames with &'s in them
@pradeep - I see the initial pop up has the D underlined - which is what a
& character would do to a string - that stinks.
but you are seeing the & character in the actual filename.
What version of IE
Re: Allow download of filenames with &'s in them
Quote:
Originally Posted by
Pradeep1210
BTW how big are your files? Why do you need to read/write in chunks?
They are huge - we do multi GB uploads and downloads
1 Attachment(s)
Re: Allow download of filenames with &'s in them
Quote:
Originally Posted by
szlamany
@pradeep - I see the initial pop up has the D underlined - which is what a
& character would do to a string - that stinks.
but you are seeing the & character in the actual filename.
What version of IE
IE Version is: 6.0 :o
Yep. That underscore appearing there looks like a bug to me. Maybe it has been fixed in the latter versions of IE, but I'm not sure. I never use it other than development work.
Attachment 78703
Re: Allow download of filenames with &'s in them
Quote:
Originally Posted by
szlamany
They are huge - we do multi GB uploads and downloads
OK. Then it makes a lot of sense to transfer file in chunks, otherwise just a handful of users can eat up all the server memory and maybe crash your server too. :)
Re: Allow download of filenames with &'s in them
Quote:
Originally Posted by
Pradeep1210
OK. Then it makes a lot of sense to transfer file in chunks, otherwise just a handful of users can eat up all the server memory and maybe crash your server too. :)
Which is exactly what was happening!