zoukankan      html  css  js  c++  java
  • Rest文件下载

     public void DownloadFile(string fileId)
            {
                //Stream fileStream = null;
                try
                {
                    int fileID = Convert.ToInt32(fileId);
                    string RelatePath = fileInfoBLL.GetRelatePath(fileID);
                    string fileFullPath = filePath + "\" + RelatePath;
                    string fileName = fileInfoBLL.GetFileName(fileID);
                    var response = HttpContext.Current.Response;
                    response.Clear();
                    response.Buffer = true;
                    //fileName = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.GetEncoding("UTF-8"));
                    response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
                    response.ContentEncoding = System.Text.Encoding.UTF8;
                    response.Charset = "UTF-8";
                    response.WriteFile(fileFullPath);
                }
                catch (Exception ex)
                {
                    ExceptionHandler.ExceptionHelper.Instance.HandleException(ex);
                }
            }

    将字符串转换成指定编码格式:

    1   string fileName = System.Web.HttpUtility.UrlEncode("要转换的字符串", System.Text.Encoding.GetEncoding("UTF-8"));

    2 string gbStr = System.Text.Encoding.GetEncoding("gb2312").GetString(System.Text.Encoding.Default.GetBytes('xxx'));

    另一种:

     1  private void DownloadFile(string fileName, string filePath)
     2         {
     3             try
     4             {
     5                 if (!string.IsNullOrEmpty(filePath))
     6                 {
     7                     if (string.IsNullOrEmpty(fileName))
     8                     {
     9                         fileName = filePath.Substring(filePath.LastIndexOf("\") + 1);
    10                     }
    11 
    12                     context.Response.Clear();
    13                     context.Response.Buffer = true;
    14                     context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(fileName));
    15                     context.Response.ContentEncoding = System.Text.Encoding.UTF8;
    16                     context.Response.Charset = "UTF-8";
    17                     //context.Response.ContentType = "application/vnd.ms-excel";
    18                     context.Response.WriteFile(UploadFileSavePath + "\" + filePath);
    19                 }
    20             }
    21             catch (Exception ex)
    22             {
    23                 ExceptionHelper.Instance.HandleException(ex);
    24                 context.Response.Write("{"bizSuccess":false,"msg":"下载文件时发生错误!"}");
    25             }
    26 
    27             context.Response.Flush();
    28             context.Response.End();
    29         }

     利用WebOperationContext.Current.OutgoingResponse下载

    public Stream DownloadFile(string token)
            {
                Stream fileStream = null;
                try
                {
                    //FilesInfo filesInfo = m_FileDAL.Find<FilesInfo>(fileId);
                    //string fileFullPath = filesInfo.FileUrl + filesInfo.FileName;
                    //string fileFullPath = m_FileDAL.Find<FilesInfo>(fileId).FileUrl;
                    //string fileFullPath = @"C:UserschenDesktop
    eader.txt";
                    string fileFullPath = @"C:UserschenDesktopHSF第四次读后感.docx";
                    //string fileName = "reader.txt";
                    string fileName = "HSF第四次读后感.docx";
                    fileStream = File.OpenRead(fileFullPath);
                    var response = WebOperationContext.Current.OutgoingResponse;
                    response.Headers.Add("Content-Disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(fileName));
                    response.ContentType = "application/octet-stream";               
                    //var message = WebOperationContext.Current.CreateStreamResponse(fileStream, "application/octet-stream");
                    //message.Headers.Add(System.ServiceModel.Channels.MessageHeader.CreateHeader("Content-Disposition", "", "attachment;filename=" + HttpUtility.UrlPathEncode(fileName)));               
                 //response.Clear();
                      //response.Buffer = true;
                      //response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(fileName));
                      //response.ContentEncoding = System.Text.Encoding.UTF8;
                      //response.Charset = "UTF-8";
                   
                    //result.Success = true;
                    //result.ErrorMessage = string.Format("{0}下载成功!", fileId);
                    //result.Data = fileStream;
                }
                catch (Exception ex)
                {
                   
                    //result.Success = false;
                    //result.ErrorMessage = string.Format("{0}下载失败!", fileId);
                    //result.Data = null;
                }
    
                return fileStream;
            }
  • 相关阅读:
    常用的网站cms内容管理系统推荐
    PageAdmin网站内容管理系统出现403错误的解决方法
    使用PageAdmin网站内容管理系统做网站的好处
    网站建设步骤及常用建站系统分享
    企业网站建设如何选择建站公司
    如何采用PageAdmin自助建站系统来进行企业网站建设
    站群系统-站群软件系统推荐
    js计算之递归
    算法之插入排序
    javaScript设计模式之常用工厂模式
  • 原文地址:https://www.cnblogs.com/lihongchen/p/3699153.html
Copyright © 2011-2022 走看看