Forum Discussion

desaiamey's avatar
desaiamey
New Member
3 years ago

Download Files using C# from Swagger UI

Hi ,

 

I am trying consume the Swagger API to download DOCX, XLSX, JPEG, TXT files using the custom applications and below is the client code to consume Swagger API.

Files are getting created but it is corrupt and does not display the content and below is the error shown when downloading excel files as attached.

 

 

C# Code - 

static public async Task HttpGetForLargeFileInRightWay()
{
SL_LandingZone_DEntities sL_LandingZone_DEntities = new SL_LandingZone_DEntities();
using (HttpClient client = new HttpClient())
{
var reqdata = (from ReqDoc in sL_LandingZone_DEntities.AX_Marad_RequisitionDocuments
// where ReqDoc.SubmittedDate is null
select ReqDoc).ToList();

foreach (var r in reqdata)
{

var accessToken = XYZ
using (HttpResponseMessage response = await client.GetAsync("http://XYZ-t.inet-local.com/GenAPI/Document/GetDocumentContentDTO/" + r.Id + "?apiKey=" + accessToken, HttpCompletionOption.ResponseHeadersRead))
using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync())
{
string fileToWriteTo = r.Url;
using (Stream streamToWriteTo = File.Open(fileToWriteTo, FileMode.Create))
{
await streamToReadFrom.CopyToAsync(streamToWriteTo);
}
}
}
}
}

 

Response Body from the API -

 

      • {
          "Result": "OK",
          "ErrorMessage": null,
          "Document": {
            "Name": "XYZ.docx",
            "Size": 1016995,
            "FileHash": "593BE38A37D38012B45145AB41500CF71C375EC866CE9F319AD723A550248789",
            "FileContent": "UEsDBBQABgAIAAAAIQCj77sdZQEAAFIFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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