16

Tôi đang cố tải xuống tệp zip từ bộ điều khiển api trên web của mình. Nó trả về tập tin nhưng tôi nhận được một tin nhắn zipfile là không hợp lệ khi tôi cố gắng để mở. Tôi đã thấy các bài viết khác về điều này và phản hồi đã thêm responseType: 'arraybuffer'. Vẫn không làm việc cho tôi. Tôi không nhận được bất kỳ lỗi nào trong bảng điều khiển.cách tải xuống tệp zip bằng cách sử dụng góc

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

khiển 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; 
     } 
    } 

Cập nhật pic

+0

Chức năng 'saveAs' là gì? – miensol

+0

là phương thức filesaver.js – texas697

Trả lời

13

Tôi nghĩ rằng bạn đang đặt nền responseType sai chỗ, thay vì điều này:

$http.post('/api/apiZipPipeLine/', model) 

Hãy thử điều này:

$http.post('/api/apiZipPipeLine/', model, {responseType:'arraybuffer'}) 

Hãy xem this answer để biết thêm chi tiết.

+0

đã hoạt động! Tuy nhiên, pdf bị hỏng? Tôi có cần phải thay đổi một cái gì đó trong bộ điều khiển api của tôi? – texas697

+0

tôi đăng một ảnh chụp màn hình của một ngoại lệ trong api của tôi. Tôi biết điều này có thể là một câu hỏi riêng nhưng nếu bạn có thể xem xét tôi sẽ đánh giá cao nó – texas697

+0

Hình như get_Length() không được hỗ trợ, có lẽ bạn cần phải tải xuống toàn bộ luồng đầu tiên? – Buddy

3

Thực tế, bạn đang thêm một cách nghiêm ngặt responseType:'arraybuffer'. Điều đó được thêm vào mã sau khi nhận được phản hồi từ ajax sẽ nhắc tệp tải xuống:

var a = document.createElement('a'); 
var blob = new Blob([responseData], {'type':"application/octet-stream"}); 
a.href = URL.createObjectURL(blob); 
a.download = "filename.zip"; 
a.click(); 
Các vấn đề liên quan