What is the correct way to upload a file using NSwag code generator (angular 2 typescript)

I am trying to upload a file via angular 2 typescript client. The link generated in the Swagger UI works fine, but the typescript client generated doesn't work.

The controller looks like this:

    [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 interface: Loading Swagger UI

The generated typescript client looks like this. As you can see, responseText is set but never returned. What am I missing?

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

      

Regards

+4


source to share


1 answer


Found the answer to this problem:

On startup add:

services.AddSwaggerGen(options =>
{   
options.MapType<FileContentResult>(() => new Schema
       {
                Type = "file",
            });
}

      



And for your controller:

[HttpPost()]
    [SwaggerResponse(200, typeof(FileContentResult))]
    [ProducesResponseType(typeof(FileContentResult), 200)]
    public async Task<FileResult> MyMethod(Viewmodel vm)
    {

      

Late answer, but for people who have the same problem ...

0


source







All Articles