zoukankan      html  css  js  c++  java
  • ASP.NET MVC5 之 客户端实现文件的下载

         MVC 实现下载功能主要借助于 File 属性:

         //下载文件接口

        public ActionResult GetTrackTempIsc(ICSModels icsModels)        

       {            

             bool flag = false;            

             string path = Server.MapPath("~/File/file.ics");            

             if (icsModels.IcsFMTTYPE.Contains("{1}br{2}"))            

             {                

                     icsModels.IcsFMTTYPE = icsModels.IcsFMTTYPE.Replace("{1}br{2}", "<br/>");          

            }            

              var trackIsc = GetTempContent(path, icsModels);                       //字符流            

             byte[] bt = System.Text.Encoding.UTF8.GetBytes(trackIsc);            

             string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".ics";//客户端保存的文件名           

            //以字符流的形式下载文件             

              return File(bt, "application/vnd.ms-txt", fileName);

         }

             /// <summary>
            /// 读取文件内容
            /// </summary>
            public string GetTempContent(string tempPath, ICSModels icsModels)
            {
                StringBuilder content = new StringBuilder();
                if (System.IO.File.Exists(tempPath))
                {
                    using (StreamReader sr = new StreamReader(tempPath, Encoding.GetEncoding("utf-8")))
                    {
                        String srLine;
                        while ((srLine = sr.ReadLine()) != null)
                        {
                            content.AppendLine(srLine);
                        }
                    }
                    // string strContent = content.ToString();
                    return ConvertTempContent(content.ToString(), icsModels);
                }
                return null;
            }
            /// <summary>
            /// 字符替换
            /// </summary>
            private string ConvertTempContent(string source, ICSModels icsModels)
            {
                if (source.Contains("#IcsCREATED#"))
                    source = source.Replace("#IcsCREATED#", icsModels.IcsCREATED);
                return source;
            }

           //PDF 文件下载

            public ActionResult GetPdfFile(PdfDownModel pdfModel)     

            {            

                 var pdfPath = Server.MapPath("~/PdfFile/outstanding.pdf");            

                 PdfFileEditor fileEditor = new PdfFileEditor();            

                 Document doc = new Document(pdfPath);            

                 PdfContentEditor pdfContentEditor = new PdfContentEditor();            

                 pdfContentEditor.BindPdf(doc);            

                 pdfContentEditor.ReplaceText("Qi Liu", pdfModel.UserName);             //保存            

                  var maPath = "~/pdfDown/" + pdfModel.UserName + ".pdf";            

                  var docPath = Server.MapPath(maPath);            

                  doc.Save(docPath);             //字符流            

                   // byte[] bt = System.Text.Encoding.UTF8.GetBytes(docPath);            

                   //string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";//客户端保存的文件名

                   // System.IO.File.Delete(docPath);            

                   string fileName = pdfModel.UserName + ".pdf";//客户端保存的文件名             //以字符流的形式下载文件             

                   return File(docPath, "application/pdf", fileName);

            }

  • 相关阅读:
    WPF and Silverlight 学习笔记:键盘输入、鼠标输入、焦点处理
    [转]Visual Studio .NET "目标平台" 说明
    WPF 使用HwndHost嵌入Win32后,无法接受Mouse_Move\Mouse_Leave消息
    c#有多少种可能导致写文件失败?
    优化性能:文本【msdn】
    解决popup不随着window一起移动的问题
    异常处理的资料
    利用bat编译WPF项目
    属性值继承
    WPF消息机制
  • 原文地址:https://www.cnblogs.com/hanxingli/p/5378728.html
Copyright © 2011-2022 走看看