zoukankan      html  css  js  c++  java
  • C# MVC Get请求下载文件代码模板(支持返回错误信息)

    // 下载流程附件
            [HttpGet, Route("TaskflowFileDownload")]
            public async Task<IHttpActionResult> TaskflowFileDownload(string taskfileId)
            {
                try
                {
                    var currentUserId = GetUserId();
                    if (string.IsNullOrEmpty(currentUserId))
                    {
                        return JsonError("未登录或登录已过期");
                    }
                    if (string.IsNullOrEmpty(taskfileId))
                    {
                        return JsonError("缺少参数");
                    }
                    var file = service.GetTaskflowFileInfo(taskfileId);
                    if (file == null)
                    {
                        return JsonError("未找到该流程附件");
                    }
                    string fileName = file.FileName;
                    string filePath = Path.Combine(new VaultFactory().GetBaseDirectory(), file.Bucket, file.NewFileName);
                    // 开始下载
                    var DownResult = VimServiceController.ExportWordByPath(fileName, filePath);
                    if (DownResult)
                    {
                        return JsonSuccess("", "下载成功");
                    }
                    return JsonError("下载失败");
                }
                catch (System.Exception e)
                {
                    LogHelper.WriteLog("下载流程附件异常", e);
                    return JsonError("下载流程附件失败");
                }
            }
    public static bool ExportWordByPath(string fileName, string filePath)
            {
                try
                {
                    var Response = HttpContext.Current.Response;
                    System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
                    if (fileInfo.Exists == true)
                    {
                        const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
                        byte[] buffer = new byte[ChunkSize];
                        Response.Clear();
                        using (System.IO.FileStream iStream = System.IO.File.OpenRead(filePath))
                        {
                            long dataLengthToRead = iStream.Length;//获取下载的文件总大小
                            Response.ContentType = "application/octet-stream";
                            Response.Charset = "UTF-8";
                            Response.AddHeader("Content-Length", iStream.Length.ToString());
                            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
                            while (dataLengthToRead > 0 && Response.IsClientConnected)
                            {
                                int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
                                Response.OutputStream.Write(buffer, 0, lengthRead);
                                Response.Flush();
                                dataLengthToRead = dataLengthToRead - lengthRead;
                            }
                            Response.Close();
                            return true;
                        }
                    }
                    return false;
                }
                catch (System.Exception ex)
                {
                    LogHelper.WriteLog(ex.Message, ex);
                    return false;
                }
            }
  • 相关阅读:
    【六校联合训练 省选 #20】快递
    IOError: cannot open resource
    [已解决]运行gunicorn失败:[ERROR] Connection in use 127.0.0.1 8080
    windows下通过navicat for mysql连接centos6.3-64bit下的MySQL数据库
    在centos7中使用yum安装mysql数据库并使用navicat连接
    centos出现“FirewallD is not running”怎么办
    [linux]centos7下解决yum install mysql-server没有可用包
    CentOS 7安装Python3
    flask——CSRFToken保护
    python 获取当前文件夹下所有文件名
  • 原文地址:https://www.cnblogs.com/sangzs/p/14153935.html
Copyright © 2011-2022 走看看