2011-04-04 125 views
0

我有一個aspx網站:文件下載與ASPX,符號添加到文件末尾

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Download.aspx.cs" Inherits="ATP.Management.Web.Download" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    ... 
    </div> 
    </form> 
</body> 
</html> 

與此代碼背後:

Response.ContentType = "application/octet-stream"; 
Response.AddHeader("Content-disposition", "attachment; filename={0}".FormatWith(filename)); 
var bytes = File.ReadAllBytes(@"c:\temp\" + filename); 
Response.BinaryWrite(bytes); 

請注意,我沒有真正從C閱讀:\ temp,這只是一些測試代碼。這工作得很好,並且文件正常傳輸,但是當我在記事本中打開文件時,似乎已將<html>....</html>下載頁面的文本附加到它。

爲什麼會發生這種情況,我該如何防止這種情況發生?

回答

1

在二進制寫入後添加Response.End()

相關問題