ASP.NET 文件下载功能源码

本文介绍了一种使用ASP.NET进行文件下载的方法,包括设置HTTP响应头以触发浏览器下载行为及处理文件不存在的情况,并列举了常见文件类型的ContentType。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

try
        {
            string FULLFILENAME = Server.MapPath();
            FileInfo downLoadFile = new FileInfo(FULLFILENAME);
            if(downLoadFile.Exists)
            {
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer=false;
                Response.ContentType="application/octet-stream";
                Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(DownloadFile.FullName,System.Text.Encoding.ASCII));

                Response.AppendHeader("Content-Length",DownloadFile.Length.ToString());
                Response.WriteFile(DownloadFile.FullName);
                Response.Flush();

                Response.End();
            }
            else{
                //文件不存在
            }
        }
        catch
        {
          //文件不存在
        } 

 

 

下面是常用的的ContentType

  'doc' => 'application/msword',

  'bin' => 'application/octet-stream',

  'exe' => 'application/octet-stream',

  'class' => 'application/octet-stream',

  'dll' => 'application/octet-stream',

  'xls' => 'application/vnd.ms-excel',

  'ppt' => 'application/vnd.ms-powerpoint',

  'vcd' => 'application/x-cdlink',

  'dir' => 'application/x-director',
  'dxr' => 'application/x-director',

  'dvi' => 'application/x-dvi',

  'spl' => 'application/x-futuresplash',

  'js' => 'application/x-javascript',

  'swf' => 'application/x-shockwave-flash', 

  'src' => 'application/x-wais-source',
  'xhtml' => 'application/xhtml+xml',

  'mp2' => 'audio/mpeg',

  'mp3' => 'audio/mpeg',

  'wav' => 'audio/x-wav',

  "wmv" => 'video/x-ms-wmv',

  "wma" => 'audio/x-ms-wma',

  'pdb' => 'chemical/x-pdb',

  'bmp' => 'image/bmp',

  'gif' => 'image/gif',

  'jpeg' => 'image/jpeg',

  'jpg' => 'image/jpeg',

  'jpe' => 'image/jpeg',

  'png' => 'image/png',

  'css' => 'text/css',

  'html' => 'text/html',

  'htm' => 'text/html',

  'asc' => 'text/plain',

  'txt' => 'text/plain',

  'rtx' => 'text/richtext',

  'rtf' => 'text/rtf',

  'xsl' => 'text/xml',

  'xml' => 'text/xml',

  'mpeg' => 'video/mpeg',

  'mpg' => 'video/mpeg',

  'avi' => 'video/x-msvideo',

  'movie' => 'video/x-sgi-movie',

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值