gpt4 book ai didi

typescript - 通过 NSwag 代码生成器(angular 2 typescript )下载文件的正确方法是什么

转载 作者:搜寻专家 更新时间:2023-10-30 20:34:55 25 4
gpt4 key购买 nike

我尝试通过 Angular 2 typescript 客户端下载文件。在 Swagger UI 中生成的链接工作正常,但生成的 typescript 客户端没有。

Controller 看起来像这样:

    [HttpGet("export")]
[SwaggerResponse((int) HttpStatusCode.OK, Type = typeof(FileContentResult))]
[ProducesResponseType(typeof(FileResult), (int) HttpStatusCode.OK)]
[Produces("text/csv")]
public virtual FileResult Export(int Id, string fileType, CsvFormat format, bool includeHeader)
{
.
.
.
FileStreamResult file = new FileStreamResult(s, "text/csv");
file.FileDownloadName = ts.Name + "." + fileType;

return file;
}

Swagger 用户界面:Swagger UI Download

生成的 typescript 客户端如下所示。如您所见,已设置 responseText 但从未返回。我错过了什么?

protected processRestTimeSeriesExportGet(response: Response): Observable<void> {
const status = response.status;

if (status === 200) {
const responseText = response.text();
return Observable.of<void>(<any>null);
} else if (status !== 200 && status !== 204) {
const responseText = response.text();
return throwException("An unexpected server error occurred.", status, responseText);
}
return Observable.of<void>(<any>null);
}

最好的问候

最佳答案

Eric Gontier 的解决方案非常适用于 Swashbuckle 4 和 NSwag 12。如果您已升级到 swashbuckle 5 并因此升级到 OpenApi 3 和 NSwag 13,则解决方案会有所不同。相反,您需要一个自定义操作过滤器和一个可重用的属性来指示内容类型结果:

自定义属性

/// <summary>
/// Indicates swashbuckle should expose the result of the method as a file in open api (see https://swagger.io/docs/specification/describing-responses/)
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class FileResultContentTypeAttribute : Attribute
{
public FileResultContentTypeAttribute(string contentType)
{
ContentType = contentType;
}

/// <summary>
/// Content type of the file e.g. image/png
/// </summary>
public string ContentType { get; }
}

操作过滤器

public class FileResultContentTypeOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
var requestAttribute = context.MethodInfo.GetCustomAttributes(typeof(FileResultContentTypeAttribute), false)
.Cast<FileResultContentTypeAttribute>()
.FirstOrDefault();

if (requestAttribute == null) return;

operation.Responses.Clear();
operation.Responses.Add("200", new OpenApiResponse
{
Content = new Dictionary<string, OpenApiMediaType>
{
{
requestAttribute.ContentType, new OpenApiMediaType
{
Schema = new OpenApiSchema
{
Type = "string",
Format = "binary"
}
}
}
}
});
}
}

启动.cs

services.AddSwaggerGen(options =>
{
...
options.OperationFilter<FileResultContentTypeOperationFilter>();
}

示例 Controller

然后用属性注释您的 Controller 。

[HttpPost]
[Route("{fileName}.csv")]
[FileResultContentType("text/csv")]
public async Task<ActionResult> Generate(string fileName, [FromBody]MyDto myDto)
{
var fileMemoryStream = GetCsvAsBytes(myDto);
return File(fileMemoryStream,
"text/csv", fileName + ".csv");
}

关于typescript - 通过 NSwag 代码生成器(angular 2 typescript )下载文件的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43844261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com