asp.net 几种文件下载方式比较

来源:互联网 发布:淘宝电商运营试题 编辑:程序博客网 时间:2024/04/30 20:22
 

/*
     * asp.net 几种文件下载方式比较
     *
     * 方法1、HttpResponse.TransmitFile
     * 方法2、HttpResponse.WriteFile
     * 方法3、HttpResponse.BinaryWrite
     * 方法4、HttpResponse.Redirect
     *
     * 方法1与方法2
     *      相同点:都是通过文件的相对或绝对路径下载文件;
     *      不同点:方法2是一次性将文件读入内存,然后输出给客户端;
     *              方法1不在内存中缓冲文件。
     *        
     *      因此,对于大文件或用户数多的下载,方法1不会对于服务器内存的占用将远远低于方法2;
     *      这正是方法1的最大优势,
     *      但方法1也有一个局限:does not work with UNC-share file paths.     *     
     *      UNC (Universal Naming Convention) / 通用命名规则,也叫通用命名规范、通用命名约定。
     *      它符合 \servername\sharename 格式
     *      也就是说方法1无法下载网络共享磁盘的文件
     *     
     *      例如:
     *      if (filePath.StartsWith(@"\\")) 
     *          context.Response.WriteFile(filePath, false);
     *      else 
     *          context.Response.TransmitFile(filePath);
     *     
     * 方法3
     *      方法3主要是将已有的btye[] 型对象输出到客户端;
     *      如果要下载的文件位于数据库等存储介质,那么,读入内存时一般可放于DataTable等对象中,
     *      这时就可以直接HttpResponse.BinaryWrite((byte[])dt.Rows[0]["fileContent"])输出
     *
     * 方法4
     *      方法4主要是通过文件的相对路径下载文件;     *
     *
     *
     * 以上四个方法,如果下载一个汉字命名且字数超过20个字的文件
     * 方法1不会有问题;
     * 使用其它三个方法下载后,如果客户端在提示框中点“打开”将报错,提示文件名过长。
     *      
     */ 以下是测试用到的 download.aspx 及 download.cs的代码:

download.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="download.aspx.cs" Inherits="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> <asp:Button ID="btnDownloadByRedirect" runat="server" 
            Text
="using Response.Redirect()" onclick="btnDownloadByRedirect_Click"
            
/>
        
<asp:Button ID="btnDownloadByTransmitFile" runat="server" Text="using Response.TransmitFile()"
            onclick
="btnDownloadByTransmitFile_Click" />
        
<asp:Button ID="btnDownloadByWriteFile" runat="server" Text="using Response.WriteFile()"
            OnClick
="btnDownloadByWriteFile_Click" />
        
<asp:Button ID="btnDownloadByBinaryWrite" runat="server" Text="using Response.BinaryWrite()"
            OnClick
="btnDownloadByBinaryWrite_Click"  />
    
</div>
    
</form>
</body>
</html>

download.cs 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;

public partial class download : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
//System.Threading.Thread.Sleep(5000);
        
//Download(HttpContext.Current.Response);
    }


    
protected void btnDownloadByRedirect_Click(object sender, EventArgs e)
    
{
        Download(HttpContext.Current.Response);
    }


    
protected void btnDownloadByTransmitFile_Click(object sender, EventArgs e)
    
{
        Download(HttpContext.Current.Response, DownloadMethod.TransmitFile);
    }


    
protected void btnDownloadByWriteFile_Click(object sender, EventArgs e)
    
{
        Download(HttpContext.Current.Response, DownloadMethod.WriteFile);
    }


    
protected void btnDownloadByBinaryWrite_Click(object sender, EventArgs e)
    
{
        Download(HttpContext.Current.Response, DownloadMethod.BinaryWrite);
    }


    
private FileInfo info = null;

    
private enum DownloadMethod
    
{
        WriteFile,
        BinaryWrite,
        TransmitFile
    }


    
private bool PrepareFile()
    
{
        
string applicationPath = HttpContext.Current.Request.PhysicalApplicationPath;
        
string lastCharacter = applicationPath.Substring(applicationPath.Length - 1);

        
if (!lastCharacter.Equals("\\"))
        
{
            applicationPath 
= applicationPath + "\\";
        }


        info 
= new FileInfo(applicationPath + "Files\\测试文件测试文件测试文件测试文件测试文件测.xls");        

        
return info.Exists;
    }


    
private byte[] GetFileByte()
    
{
        
return File.ReadAllBytes(info.FullName);
    }


    
private long GetFileLength()
    
{
        
return info.Length;
    }


    
private void Download(HttpResponse response)
    
{
        
if (PrepareFile())
        
{
            response.Redirect(
"Files/" + info.Name, true);
        }

    }


    
private void Download(HttpResponse response, DownloadMethod method)
    
{
        
if (PrepareFile())
        
{
            response.Clear();
            response.ClearHeaders();
            response.Buffer 
= false;

           
string displayName = HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(info.Name));

            response.AppendHeader(
"Content-Disposition""attachment;filename=" + displayName);
            response.ContentType 
= "application/octet-stream";

            
switch (method)
            
{
                
case DownloadMethod.WriteFile:
                    
//response.WriteFile(info.FullName);
                    response.TransmitFile("Files/" + info.Name);
                    
break;

                
case DownloadMethod.BinaryWrite:
                    response.BinaryWrite(GetFileByte());
                    
break;

                
case DownloadMethod.TransmitFile:
                    
//response.TransmitFile(info.FullName);
                    response.TransmitFile("Files/" + info.Name);
                    
break;
            }


            response.Flush();
            response.End();
        }
 
    }
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 带着节育环做的核磁怎么办 便秘洗肠后最一周未排便怎么办 用了开塞露后肚子疼拉不出来怎么办 冰点脱毛当天用沐浴露洗澡了怎么办 自体脂肪填充脸部但发红又痒怎么办 金矿受伤死亡不给开死亡证明怎么办 手机欠费了导致没信号了怎么办 金立手机指纹硬件无法使用怎么办 试管取卵医生说卵子碎片多怎么办 取卵腹水抽水后尿不通怎么办 手机锁屏密码忘了怎么办求解锁 苹果手表锁屏密码忘记了怎么办 苹果手表锁屏密码忘了怎么办 电脑输密码时点了用户账户怎么办 w7电脑锁屏密码忘记了怎么办 台式电脑w7锁屏密码忘记了怎么办 win7电脑锁屏密码忘记了怎么办 苹果手机4s开机密码忘记了怎么办 苹果4s下载东西忘记密码怎么办 苹果4s不记得开机密码怎么办? 苹果手机id密码忘了怎么办能解锁 苹果5s id密码忘了怎么办? 苹果手机激活锁id忘记了怎么办 苹果刷了机忘了账号无法激活怎么办 三星s7指纹解开锁密码忘了怎么办 索尼手机锁屏密码忘了怎么办 金立手机开机密码忘了怎么办 如果小米手机锁屏密码忘记了怎么办 小米手机锁屏密码忘了怎么办 小米5x忘记了屏保锁屏密码怎么办 htc手机锁屏密码忘了怎么办 苹果7手机解锁密码忘了怎么办 魅族7plus锁屏密码忘了怎么办 捡到苹果手机不知道id密码怎么办 平板不知道id地址和密码怎么办 红米1s刷机变砖了怎么办 车玻璃被鞭炮炸了黑印子怎么办 出轨的事被家人知道后道处传怎么办 村霸霸占土地弱势村民该怎么办? 户户通没有插卡位置信息改变怎么办 出现重大污染天气时企业该怎么办