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);

            }

  • 相关阅读:
    使用JDBC连接MySql时出现:The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration
    Mysql Lost connection to MySQL server at ‘reading initial communication packet', system error: 0
    mysql-基本命令
    C# 监听值的变化
    DataGrid样式
    C# 获取当前日期时间
    C# 中生成随机数
    递归和迭代
    PHP 时间转几分几秒
    PHP 根据整数ID,生成唯一字符串
  • 原文地址:https://www.cnblogs.com/hanxingli/p/5378728.html
Copyright © 2011-2022 走看看