angularjs – 如何使用角度下载zip文件
发布时间:2020-12-17 07:45:26 所属栏目:安全 来源:网络整理
导读:我正在尝试从我的web api控制器下载zip文件.它正在返回文件,但是当我尝试打开时,我收到一个消息,zipfile无效.我已经看到关于这个的其他帖子,响应添加了responseType:’arraybuffer’.仍然不为我工作我也没有在控制台任何错误. var model = $scope.selection
我正在尝试从我的web api控制器下载zip文件.它正在返回文件,但是当我尝试打开时,我收到一个消息,zipfile无效.我已经看到关于这个的其他帖子,响应添加了responseType:’arraybuffer’.仍然不为我工作我也没有在控制台任何错误.
var model = $scope.selection; var res = $http.post('/api/apiZipPipeLine/',model) res.success(function (response,status,headers,config) { saveAs(new Blob([response],{ type: "application/octet-stream",responseType: 'arraybuffer' }),'reports.zip'); notificationFactory.success(); }); api控制器 [HttpPost] [ActionName("ZipFileAction")] public HttpResponseMessage ZipFiles([FromBody]int[] id) { if (id == null) {//Required IDs were not provided throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)); } List<Document> documents = new List<Document>(); using (var context = new ApplicationDbContext()) { foreach (int NextDocument in id) { Document document = context.Documents.Find(NextDocument); if (document == null) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); } documents.Add(document); } var streamContent = new PushStreamContent((outputStream,httpContext,transportContent) => { try { using (var zipFile = new ZipFile()) { foreach (var d in documents) { var dt = d.DocumentDate.ToString("y").Replace('/','-').Replace(':','-'); string fileName = String.Format("{0}-{1}-{2}.pdf",dt,d.PipeName,d.LocationAb); zipFile.AddEntry(fileName,d.DocumentUrl); } zipFile.Save(outputStream); //Null Reference Exception } } finally { outputStream.Close(); } }); streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); streamContent.Headers.ContentDisposition.FileName = "reports.zip"; var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = streamContent }; return response; } } 更新
我想你将responseType设置在错误的地方,而不是这样:
$http.post('/api/apiZipPipeLine/',model) 尝试这个: $http.post('/api/apiZipPipeLine/',model,{responseType:'arraybuffer'}) 看看this answer了解更多细节. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- twitter-bootstrap – list-group-item中的可点击按钮或gly
- Scala`s <>运算符含义
- MDT 2010 - Setting the Computer Description in AD witho
- weblogic 部署 AXIS WEBSERVICE 环境问题解决方案.
- 当errexit选项设置时,检测shell退出代码的正确方法
- 【数据结构】二叉搜索树的递归与非递归实现
- REST WebService 通过IP过滤和签名字段来增强安全
- twitter-bootstrap – 按引导顺序调整大小按钮
- bootstrap 分页学习笔记
- 更改焦点以输入AngularJS中的关键事件