1

我想从dotnet核心web api动作下载一个zip文件,但是我无法使它工作。我试着通过POSTMAN和我的Aurelia Http Fetch Client调用动作。如何从dotnet核心webapi下载ZipFile?

我能够像我想要的那样创建ZipFile并将其存储在系统中,但无法修复它,因此它通过api返回zipfile。

用例:用户选择几个图片集并单击下载按钮。图片集的id被发送到api,并且创建一个zipfile文件,其中包含用于保存图片的每个图片集的目录。该zipfile被返回给用户,以便他/她可以将其存储在他们的系统上。

任何帮助,将不胜感激。

我的控制器动作

 /// <summary> 
     /// Downloads a collection of picture collections and their pictures 
     /// </summary> 
     /// <param name="ids">The ids of the collections to download</param> 
     /// <returns></returns> 
     [HttpPost("download")] 
     [ProducesResponseType(typeof(void), (int) HttpStatusCode.OK)] 
     public async Task<IActionResult> Download([FromBody] IEnumerable<int> ids) 
     { 
      // Create new zipfile 
      var zipFile = $"{_ApiSettings.Pictures.AbsolutePath}/collections_download_{Guid.NewGuid().ToString("N").Substring(0,5)}.zip"; 

      using (var repo = new PictureCollectionsRepository()) 
      using (var picturesRepo = new PicturesRepository()) 
      using (var archive = ZipFile.Open(zipFile, ZipArchiveMode.Create)) 
      { 
       foreach (var id in ids) 
       { 
        // Fetch collection and pictures 
        var collection = await repo.Get(id); 
        var pictures = await picturesRepo 
          .GetAll() 
          .Where(x => x.CollectionId == collection.Id) 
          .ToListAsync(); 

        // Create collection directory IMPORTANT: the trailing slash 
        var directory = $"{collection.Number}_{collection.Name}_{collection.Date:yyyy-MM-dd}/"; 
        archive.CreateEntry(directory); 

        // Add the pictures to the current collection directory 
        pictures.ForEach(x => archive.CreateEntryFromFile(x.FilePath, $"{directory}/{x.FileName}")); 
       } 
      } 

      // What to do here so it returns the just created zip file? 
     } 
} 

我奥里利亚获取客户端功能:

/** 
* Downloads all pictures from the picture collections in the ids array 
* @params ids The ids of the picture collections to download 
*/ 
download(ids: Array<number>): Promise<any> { 
    return this.http.fetch(AppConfiguration.baseUrl + this.controller + 'download', { 
     method: 'POST', 
     body: json(ids) 
    }) 
} 

我已经试过

需要注意的是什么,我已经试过呢不会产生错误,它似乎不是 做任何事。

1)创建我自己的FileResult(就像我以前用老版本的ASP.NET做的那样)。当我通过邮递员或应用程序调用它时,根本看不到正在使用的标题。

return new FileResult(zipFile, Path.GetFileName(zipFile), "application/zip"); 

public class FileResult : IActionResult 
{ 
     private readonly string _filePath; 
     private readonly string _contentType; 
     private readonly string _fileName; 

     public FileResult(string filePath, string fileName = "", string contentType = null) 
     { 
      if (filePath == null) throw new ArgumentNullException(nameof(filePath)); 

      _filePath = filePath; 
      _contentType = contentType; 
      _fileName = fileName; 
     } 

     public Task ExecuteResultAsync(ActionContext context) 
     { 
      var response = new HttpResponseMessage(HttpStatusCode.OK) 
      { 
       Content = new ByteArrayContent(System.IO.File.ReadAllBytes(_filePath)) 
      }; 

      if (!string.IsNullOrEmpty(_fileName)) 
       response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") 
       { 
        FileName = _fileName 
       }; 

      response.Content.Headers.ContentType = new MediaTypeHeaderValue(_contentType); 

      return Task.FromResult(response); 
     } 
} 

}

2)https://stackoverflow.com/a/34857134/2477872

不执行任何操作。

 HttpContext.Response.ContentType = "application/zip"; 
      var result = new FileContentResult(System.IO.File.ReadAllBytes(zipFile), "application/zip") 
      { 
       FileDownloadName = Path.GetFileName(zipFile) 
      }; 
      return result; 

我试过用一个测试虚拟PDF文件,这似乎与邮递员一起工作。但是,当我尝试将其更改为zip文件(请参阅上文)时,它什么都不做。

HttpContext.Response.ContentType = "application/pdf"; 
      var result = new FileContentResult(System.IO.File.ReadAllBytes("THE PATH/test.pdf"), "application/pdf") 
      { 
       FileDownloadName = "test.pdf" 
      }; 

      return result; 

回答

5

为了把长话短说,下面的例子说明了如何轻松通过dotnet的核心API服务既是一个PDF以及一个ZIP:

/// <summary> 
/// Serves a file as PDF. 
/// </summary> 
[HttpGet, Route("{filename}/pdf", Name = "GetPdfFile")] 
public IActionResult GetPdfFile(string filename) 
{ 
    const string contentType = "application/pdf"; 
    HttpContext.Response.ContentType = contentType; 
    var result = new FileContentResult(System.IO.File.ReadAllBytes(@"{path_to_files}\file.pdf"), contentType) 
    { 
     FileDownloadName = $"{filename}.pdf" 
    }; 

    return result; 
} 

/// <summary> 
/// Serves a file as ZIP. 
/// </summary> 
[HttpGet, Route("{filename}/zip", Name = "GetZipFile")] 
public IActionResult GetZipFile(string filename) 
{ 
    const string contentType ="application/zip"; 
    HttpContext.Response.ContentType = contentType; 
    var result = new FileContentResult(System.IO.File.ReadAllBytes(@"{path_to_files}\file.zip"), contentType) 
    { 
     FileDownloadName = $"{filename}.zip" 
    }; 

    return result; 
} 

此示例只是工作™

注意在这种情况下,两个操作(当然是源文件名称)只有一个主要区别:返回的contentType。

上面的例子使用'application/zip',就像你自己提到的一样,但它可能只是需要服务于不同的mimetype(比如'application/octet *')。

这导致推测无法正确读取zip文件,或者您的web服务器配置可能没有正确配置以供服务.zip文件。

后者可能会因您是否运行IIS Express,IIS,红隼等而有所不同。但要进行测试,您可以尝试向〜/ wwwroot文件夹添加zip文件,确保启用了服务您的Status.cs中的静态文件,以查看您是否可以直接下载文件。