zoukankan      html  css  js  c++  java
  • 流方式下载文件

    public ActionResult DownLoadAttachment(string attachmentId)
            {
                Attachment attachment = _customerInfoService.GetAttachment(attachmentId);
                if (attachment != null)
                {
                    try
                    {
                        string fileName = attachment.FileName;
                        string filePath = attachment.Path;
                        string fileType = attachment.FileType;
                        #region 流方式下载文件
                        Encoding encoding;
                        string outputFileName = null;
                        fileName = fileName.Replace("'", "");
    
                        string browser = Request.UserAgent.ToUpper();
                        if (browser.Contains("MS") == true && browser.Contains("IE") == true)
                        {
                            outputFileName = HttpUtility.UrlEncode(fileName);
                            encoding = Encoding.Default;
                        }
                        else if (browser.Contains("FIREFOX") == true)
                        {
                            outputFileName = fileName;
                            encoding = Encoding.GetEncoding("GB2312");
                        }
                        else
                        {
                            outputFileName = HttpUtility.UrlEncode(fileName);
                            encoding = Encoding.Default;
                        }
                        FileStream fs = new FileStream(filePath, FileMode.Open);
                        long length = fs.Length;
                        byte[] bytes = new byte[(int)fs.Length];
                        fs.Read(bytes, 0, bytes.Length);
                        fs.Close();
                        Response.Charset = "UTF-8";
                        Response.ContentType = "application/octet-stream";
                        Response.ContentEncoding = encoding;
                        Response.AddHeader("Content-Disposition", "attachment; filename=" + outputFileName);
                        Response.BinaryWrite(bytes);
                        Response.Flush();
                        Response.End();
                        return new EmptyResult();
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        return Content(string.Format("附件下载失败!错误信息:{0}", ex.Message));
                    }
    
                }
                else
                {
                    return Content("附件不存在!");
                }
            }

    ASP.NET Core 中的fileresult对象

    [HttpGet("DownLoadAttachment")]
            public FileResult DownLoadAttachment(string attachmentId)
            {
                Attachment attachment = _customerInfoService.GetAttachment(attachmentId);
                string fileName = attachment.FileName;
                string filePath = attachment.Path;
                string fileType = attachment.FileType;
                if (!System.IO.File.Exists(filePath)) return null;
    
                var stream = System.IO.File.OpenRead(filePath);  //创建文件流
    
                return File(stream, fileType, fileName);
            }
    

      

  • 相关阅读:
    数据结构C语言实现----入栈操作
    数据结构C语言实现----创建一个栈
    数据结构C语言实现----销毁链表
    数据结构C语言实现----从链表中删除结点
    数据结构C语言实现----向链表中插入结点
    数据结构C语言实现----创建一个链表
    《C程序设计语言》 练习3-5
    交易是如何被创建和打包的5
    交易是如何被创建和打包的6
    交易是如何被创建和打包的4
  • 原文地址:https://www.cnblogs.com/zhangzhang1118/p/11792831.html
Copyright © 2011-2022 走看看