I am using an API that has the capability of downloading a tif file. What I am doing is getting the results of the Async request and then copying the stream into a file on my desktop. Whenever I open the file using IrfanView, it would say that it has unrecognized headers.
My steps in retrieving this data is:
First, I would get the data by using the ReadAsStreamAsync()
function. The variable that is holding the contents of the retrieved data is of type Stream
. I would then open a filestream and use the CopyTo()
function to copy the data from the result to the file. Is this the correct way in doing this?
dynamic resultContent = null;
Stream result = null;
var queryString = args.Length == 0 ? "" : "?" + string.Join("&", args);
var url = _cfg.Server.apiUrl + _apiUri + queryString;
if (_apiOp == Operation.Read && !queryString.Contains("id"))
resultContent = this.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
else if (_apiOp == Operation.Write)
resultContent = this.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
else if (_apiOp == Operation.Delete)
resultContent = this.DeleteAsync(url).Result.Content.ReadAsStringAsync().Result;
else if (_apiOp == Operation.Read && queryString.Contains("id"))
result = this.GetAsync(url).Result.Content.ReadAsStreamAsync().Result;
if (queryString.Contains("id"))
{
var fileStream = File.Create(@"C:\Users\wei\Desktop\MyGeoTiff.tiff");
result.CopyTo(fileStream);
fileStream.Close();
}