C#文件流下载

            if (File.Exists(filePath))
            {
                string ext = Path.GetExtension(filePath).ToLower();//.html
                if ((new string[] { ".jpg", ".jpeg", ".gif", ".bmp", ".png" }).Contains(ext))
                {
                    context.Response.Clear();
                    context.Response.ContentType = "image/jpeg";
                    context.Response.BinaryWrite(GetImageBytes(filePath, context));
                    context.Response.Flush();
                    context.Response.End();
                }
                else if ((new string[] { ".mp4", ".mp3", ".rm", ".wav", ".mid", ".wmv", ".avi", ".rmvb", ".mpeg", ".mpg", ".ogg" }).Contains(ext))
                {
                    context.Response.Clear();
                    context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath),
                        System.Text.Encoding.UTF8));
                    context.Response.ContentType = "video/mpeg4";
                    context.Response.BinaryWrite(GetImageBytes(filePath, context));
                    context.Response.Flush();
                    context.Response.End();
                }
                else if ((new string[] { ".htm", ".html", "" }).Contains(ext))
                {
                    context.Response.WriteFile(filePath);
                }
                else if ((new string[] { ".xlsx", ".xls" }).Contains(ext))
                {
                    context.Response.Clear();
                    context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath),
                        System.Text.Encoding.UTF8));
                    context.Response.ContentType = "application/ms-excel";
                    context.Response.TransmitFile(filePath);
                    context.Response.Flush();
                    context.Response.End();
                }
                else if ((new string[] { ".docx", ".doc" }).Contains(ext))
                {
                    context.Response.Clear();
                    context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath),
                        System.Text.Encoding.UTF8));
                    context.Response.ContentType = "application/msword";
                    context.Response.TransmitFile(filePath);
                    context.Response.Flush();
                    context.Response.End();
                }
                else if ((new string[] { ".txt" }).Contains(ext))
                {
                    context.Response.Clear();
                    context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath),
                        System.Text.Encoding.UTF8));
                    context.Response.ContentType = "text/plain";
                    context.Response.TransmitFile(filePath);
                    context.Response.Flush();
                    context.Response.End();
                }
                else if ((new string[] { ".pdf" }).Contains(ext))
                {
                    context.Response.Clear();
                    context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath),
                        System.Text.Encoding.UTF8));
                    context.Response.ContentType = "application/pdf";
                    context.Response.TransmitFile(filePath);
                    context.Response.Flush();
                    context.Response.End();
                }
                else if ((new string[] { ".ppt", ".pptx" }).Contains(ext))
                {
                    context.Response.Clear();
                    context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath),
                        System.Text.Encoding.UTF8));
                    context.Response.ContentType = "application/vnd.ms-powerpoint";
                    context.Response.TransmitFile(filePath);
                    context.Response.Flush();
                    context.Response.End();
                }
                else
                {
                    context.Response.ContentType = "application/x-zip-compressed";
                    context.Response.TransmitFile(filePath);
                }
            }
            else
            {
                context.Response.Write("file no found");
            }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值