gpt4 book ai didi

angularjs - 如何下载一个zip文件

转载 作者:行者123 更新时间:2023-12-03 12:49:43 26 4
gpt4 key购买 nike

我正在尝试从Web API Controller 下载一个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 Controller
 [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了解更多详细信息。

关于angularjs - 如何下载一个zip文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30158115/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com