从ASP .Net下载文件时,文本文件将附加HTML内容 [英] When downloading a file from ASP .Net, the text file gets appended with HTML content

查看:168
本文介绍了从ASP .Net下载文件时,文本文件将附加HTML内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个页面,允许用户使用FileUpload Control将文件上传到服务器,并使用此代码处理其事件

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).

现在可以下载文件返回给用户(以后)我写了另一个页面,该页面从Request.Parameter(文件)中读取文件名,并提取该文件以下载给用户。我已经编写了Download.aspx页来处理ASP部分中的下载(未使用任何背后的代码):

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>

我尝试上传图像文件,然后再次下载它们,效果很好。但是,仅当我上传文本文件时,我才获得该文件的内容并附加了一些HTML内容。

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>

我转到服务器上的文件并打开了它,以确保存在附加的HTML内容,但正如我所说,该文件已完美上传。只有下载后,它才会包含附加的HTML内容。

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.

我做错了什么?我怎样做才能使这些附加的HTML代码消失?
为什么此问题仅影响文本文件,而不影响图像,EXE,XLS,DOC等?

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?

推荐答案

确保在 Response.WriteFile(filepath)之后调用 Response.End()结束您的响应,否则您的表单

Make sure to end your Response by calling Response.End() after Response.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.

这篇关于从ASP .Net下载文件时,文本文件将附加HTML内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆