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;
                }
            }
  • 相关阅读:
    cocos2dx-lua捕获用户touch事件的几种方式
    Java并发编程之闭锁CountDownLatch简单介绍
    opencv视频播放
    完全备份、差异备份以及增量备份的区别
    如何实现文件增量同步——算法
    Oracle提示“资源正忙,需指定nowait”的解决方案
    oracle之报错:ORA-00054: 资源正忙,要求指定 NOWAIT
    一次oracle大量数据删除经历
    rownum的使用-分页
    sql语句分页多种方式ROW_NUMBER()OVER
  • 原文地址:https://www.cnblogs.com/sangzs/p/14153935.html
Copyright © 2011-2022 走看看