mardi 7 août 2018

Can't download the document through Swagger

I've implemented downloading files API and I'm trying to test it in Swagger. Swagger returns a download file link, but if I download the document from this link, file is invalid. And the size of this more than should be (for example, image weight 10 KB under downloading through Swagger weighs 18 KB). At the same time, the file is correctly displayed in the network preview in browser and if I download the file simply by API method link (without using Swagger), it is also downloaded correctly. Tell me what I'm doing wrong with Swagger?

The code I use to download the file:

public static class HttpHelper
{
    public static HttpResponseMessage DocumentFileInfoModelToHttpResponseMessage(DocumentFileInfoModel model)
    {
        var message = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(model.Content)
        };

        message.Content.Headers.ContentDisposition =
            new ContentDispositionHeaderValue("attachment") {FileName = model.Name};
        message.Content.Headers.ContentType = new MediaTypeHeaderValue(model.ContentType);
        message.Content.Headers.ContentLength = model.Content.LongLength;

        return message;
    }
}

    [HttpGet]
    [AllowAnonymous]
    [SwaggerOperation(Tags = new[] {Tag})]
    public async Task<IHttpActionResult> DownloadDocument(CancellationToken cancellationToken,
        [FromUri] DocumentType documentType, [FromUri] string name = null)
    {
        var model = new DownloadDocumentModel
        {
            DocumentType = documentType,
            DocumentName = name
        };

        var result = GetDocumentFileInfo(model);

        return ResponseMessage(HttpHelper.DocumentFileInfoModelToHttpResponseMessage(result));
    }

Aucun commentaire:

Enregistrer un commentaire