Ich versuche, eine ZIP-Datei von meinem Web-API-Controller herunterladen. Es gibt die Datei zurück, aber ich erhalte eine Nachricht, dass die ZIP-Datei ungültig ist, wenn ich versuche, sie zu öffnen. Ich habe andere Beiträge darüber gesehen und die Antwort hat den responseType: 'arraybuffer' hinzugefügt. Funktioniert immer noch nicht für mich. Ich bekomme auch keine Fehler in der Konsole.wie eine Zip-Datei mit eckigen herunterladen
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;
}
}
aktualisieren
Was ist in der 'saveAs' Funktion? – miensol
ist es eine filesaver.js Methode – texas697