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

 

posted @ 2020-12-18 11:11  最好的年纪  阅读(371)  评论(0编辑  收藏  举报