Elisheva Cowan

Elisheva Cowan

  • 1.9k
  • 18
  • 197

file returning from webApi with gibberish characters

Mar 4 2020 7:46 AM
receiving file from webApi with gibberish characters when file contains characters in different language.
I tried setting encoding on header but not helping. Please advise.
 
var dataBytes = File.ReadAllBytes(result.ReturnValue.ToString());
string fileName = Path.GetFileName(result.ReturnValue.ToString());
var dataStream = new MemoryStream(dataBytes);
HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
httpResponseMessage.Content = new StreamContent(dataStream);
httpResponseMessage.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
httpResponseMessage.Content.Headers.ContentDisposition.FileName = fileName;
httpResponseMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
httpResponseMessage.Content.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
//httpResponseMessage.Content.Headers.Add("Content-Transfer-Encoding", "utf-8");
//httpResponseMessage.Content.Headers.Add("charset", Encoding.ASCII.ToString());
//httpResponseMessage.Content.Headers.Add("charset", "utf-8");
//httpResponseMessage.Content.Headers.ContentEncoding.Add("gzip");
httpResponseMessage.Content.Headers.ContentEncoding.Add("utf-8");
return httpResponseMessage;
 

Answers (17)