我想從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;