在asp.net 5中创建了wep api.我想回复Post请求的文件响应.但不是文件,响应看起来像`
{
"version": {
"major": 1,
"minor": 1,
"build": -1,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"content": {
"headers": [
{
"key": "Content-Disposition",
"value": [
"attachment; filename=test.pdf"
]
},
{
"key": "Content-Type",
"value": [
"application/pdf"
]
}
]
},
"statusCode": 200,
"reasonPhrase": "OK",
"headers": [],
"requestMessage": null,
"isSuccessStatusCode": true
}`
Run Code Online (Sandbox Code Playgroud)
码:
public HttpResponseMessage Post([FromBody]DocumentViewModel vm)
{
try
{
if (ModelState.IsValid)
{
var Document = _repository.GetDocumentByGuid(vm.DocumentGuid, User.Identity.Name);
var Params = Helper.ClientInputToRealValues(vm.Parameters, Document.DataFields);
var file = Helper.GeneratePdf(Helper.InsertValues(Params, Document.Content));
var …Run Code Online (Sandbox Code Playgroud) 我试图从dotnet核心web api操作下载一个zip文件,但我无法使其工作.我尝试通过POSTMAN和我的Aurelia Http Fetch Client调用该操作.
我能够像我想要的那样创建ZipFile并将其存储在系统上,但无法修复它,因此它通过api返回zip文件.
用例:用户选择几个图片集并单击下载按钮.图片集的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 …Run Code Online (Sandbox Code Playgroud) download zipfile asp.net-web-api .net-core aurelia-fetch-client