开发者

File download with aspx, Symbols added to end of file

开发者 https://www.devze.com 2023-02-21 18:01 出处:网络
I have an aspx site: <%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"Download.aspx.cs\" Inherits=\"ATP.Management.Web.Download\" %>

I have an aspx site:

<%@ 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>

with this code behind:

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

Please note that I am not r开发者_JS百科eally reading from c:\temp, this is just some test code. This works very well, and the file transfers properly, but when I open the file in notepad, it seems to have the <html>....</html> text of the download page attached to it.

Why is that happening and how can I prevent this?


Add Response.End() after the binary write.

0

精彩评论

暂无评论...
验证码 换一张
取 消