I have made a page that allows users to upload files to the server using a FileUpload Control and handling its event with this code
Sub SaveAttachment()
Dim root As String = "C:\temp\"
Dim filename As String = FileUpload1.FileName
Dim SaveName As String = root & filename
FileUpload1.SaveAs(SaveName)
End Sub
That worked fine, I was able to see files getting uploaded, and the content of the files is intact (exactly a duplicate copy of the file that the user's upload).
Now for downloading the files back to the user (at a later time) I have written another page that reads the file name from a Request.Parameter ("file") and fetches that file to be downloaded to the user. I have written the Download.aspx page to handle downloading in the ASP part (no code behind was used):
<%@ Import Namespace="System.IO"%>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim root As String = "C:\temp\"
Dim filepath As String = Request.Params("file")
If Not filepath Is Nothing Then
filepath = root & filepath
If File.Exists(filepath) And filepath.StartsWith(root) Then
Dim filename As String = Path.GetFileName(filepath)
Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", _
"attachment; filename=""" & filename & """")
Response.Flush()
Response.WriteFile(filepath)
End If
End If
End Sub
</script>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
I tried uploading images files and then downloading them again, and it worked fine. However, only when I upload text files that I get the content of that file appended with some HTML content.
Here is a sample file that I have uploaded
Here is my sample text file
It consists of 3 lines only
And here is the file when I downloaded it back
Here is my sample text file
It consists of 3 lines only
<form name="form1" method="post" action="FileDownload.aspx?file=sample_file.txt" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE5NTU5ODQyNTBkZNCYbOVZJDRUAOnIppQYkwHUSlb0" />
</div>
<span id="Label1">Label</span>
</form>
I went to the file on the server and opened it to make sure that the additional HTML content was there, but as I said, the file was uploaded perfectly. Only when it is downloaded does it contain appended HTML stuff.
What is it that I am doing wrong? What can I do to make this additional HTML code go away? Why does this problem only affect Text file, not images, EXE, XLS, DOC ,, etc?
Make sure to end your Response by calling
Response.End()
afterResponse.WriteFile(filepath)
or else your form will be appended to the stream and sent to the client.It will affect other files as well though most likely only be seen as garbage at the end of the file and ignored by their respective applications.
Response.redirect threw an exception. Shoaib's answer got me closer but it suppressed everything including the valid file content. What worked for me was reordering the statements as follows:
Is is not safe to use
Response.End()
, It will always return aSystem.Threading.ThreadAbortException
:.. if debugged and seen in the catch block...Alternative.. Use the following instead...