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

        }

 

posted @ 2016-04-11 15:48  李寒星  阅读(1342)  评论(0编辑  收藏  举报