<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>海鸟域 &#187; 下载</title>
	<atom:link href="http://www.silenk.com/tag/%e4%b8%8b%e8%bd%bd/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.silenk.com</link>
	<description>My territory, My way —— No Obstacle！</description>
	<lastBuildDate>Wed, 28 Apr 2010 12:28:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ASP.NET文件下载的几种方法</title>
		<link>http://www.silenk.com/asp-net-method-to-download/</link>
		<comments>http://www.silenk.com/asp-net-method-to-download/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 09:28:40 +0000</pubDate>
		<dc:creator>silenkee</dc:creator>
				<category><![CDATA[Asp.Net[C#]]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[下载]]></category>

		<guid isPermaLink="false">http://www.silenk.com/?p=64</guid>
		<description><![CDATA[    当服务器要提供文件下载时，HttpResponse有这么几种方法可以使用。

用Response.WriteFile，如：

1
2
Response.ContentType = &#34;application/octet-stream&#34;;
Response.WriteFile&#40;@&#34;whatever.zip&#34;&#41;;

采用aspnet2.0的新方法 Response.TransmitFile,注意此方法将指定的文件直接写入 HTTP 响应输出流，而不在内存中缓冲该文件。如：

1
2
3
Response.ContentType = &#34;application/x-zip-compressed&#34;;
Response.AddHeader&#40;&#34;Content-Disposition&#34;, &#34;attachment;filename=downloadfilename.zip&#34;&#41;;
Response.TransmitFile&#40;@&#34;whatever.zip&#34;&#41;;

(假设同文件夹下有个需要下载的文件叫whatever.zip，而用户下载时默认名称为downloadfilename.zip)
需要注意的是，我们都知道Server.ScriptTimeout 的默认值是90秒，而当我们在web.config中打开调试模式，此值变为30，000，000秒。这也是为什么我在开发时一般不会发现超时问题。当下载大文件时，用Response.WriteFile会使Aspnet_wp.exe缓存了太大空间而导致下载失败。
这时建议采用文件流形式。如：

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
System.IO.Stream iStream = null;
&#160;
        //以10K为单位缓存:
        byte&#91;&#93; buffer = new Byte&#91;10000&#93;;
&#160;
       int length;
&#160;
       long dataToRead;
&#160;
   [...]]]></description>
			<content:encoded><![CDATA[<p>    当服务器要提供文件下载时，HttpResponse有这么几种方法可以使用。<span id="more-64"></span></p>
<ol>
<li>用Response.WriteFile，如：</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">Response.<span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/octet-stream&quot;</span><span style="color: #008000;">;</span>
Response.<span style="color: #0000FF;">WriteFile</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;whatever.zip&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<li>采用aspnet2.0的新方法 Response.TransmitFile,注意此方法将指定的文件直接写入 HTTP 响应输出流，而不在内存中缓冲该文件。如：</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">Response.<span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/x-zip-compressed&quot;</span><span style="color: #008000;">;</span>
Response.<span style="color: #0000FF;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Content-Disposition&quot;</span>, <span style="color: #666666;">&quot;attachment;filename=downloadfilename.zip&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Response.<span style="color: #0000FF;">TransmitFile</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;whatever.zip&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>(假设同文件夹下有个需要下载的文件叫whatever.zip，而用户下载时默认名称为downloadfilename.zip)</p>
<li>需要注意的是，我们都知道Server.ScriptTimeout 的默认值是90秒，而当我们在web.config中打开调试模式，此值变为30，000，000秒。这也是为什么我在开发时一般不会发现超时问题。当下载大文件时，用Response.WriteFile会使Aspnet_wp.exe缓存了太大空间而导致下载失败。</li>
<p>这时建议采用文件流形式。如：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">Stream</span> iStream <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">//以10K为单位缓存:</span>
        <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> buffer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">Byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">10000</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
       <span style="color: #FF0000;">int</span> length<span style="color: #008000;">;</span>
&nbsp;
       <span style="color: #FF0000;">long</span> dataToRead<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// 制定文件路径.</span>
        <span style="color: #FF0000;">string</span> filepath <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;D:\mybigfile.zip&quot;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">//  得到文件名.</span>
        <span style="color: #FF0000;">string</span> filename <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">Path</span>.<span style="color: #0000FF;">GetFileName</span><span style="color: #000000;">&#40;</span>filepath<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">try</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// 打开文件.</span>
            iStream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">FileStream</span><span style="color: #000000;">&#40;</span>filepath, <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">FileMode</span>.<span style="color: #0000FF;">Open</span>,
                        <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">FileAccess</span>.<span style="color: #0000FF;">Read</span>, <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">FileShare</span>.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
&nbsp;
            <span style="color: #008080; font-style: italic;">// 得到文件大小:</span>
            dataToRead <span style="color: #008000;">=</span> iStream.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
&nbsp;
            Response.<span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/octet-stream&quot;</span><span style="color: #008000;">;</span>
            Response.<span style="color: #0000FF;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Content-Disposition&quot;</span>, <span style="color: #666666;">&quot;attachment; filename=&quot;</span><span style="color: #008000;">+</span>filename<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>dataToRead <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">//保证客户端连接</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Response.<span style="color: #0000FF;">IsClientConnected</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                   length <span style="color: #008000;">=</span> iStream.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span>buffer, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">10000</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                   Response.<span style="color: #0000FF;">OutputStream</span>.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>buffer, <span style="color: #FF0000;">0</span>, length<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    Response.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    buffer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">Byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">10000</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
                    dataToRead <span style="color: #008000;">=</span> dataToRead <span style="color: #008000;">-</span> length<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0600FF;">else</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #008080; font-style: italic;">//结束循环</span>
                    dataToRead <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// 出错.</span>
            Response.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Error : &quot;</span> <span style="color: #008000;">+</span> ex.<span style="color: #0000FF;">Message</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">finally</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>iStream <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">//关闭文件</span>
                iStream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.silenk.com/asp-net-method-to-download/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
