0

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();
            }
TheBlueMan
  • 316
  • 1
  • 4
  • 27
  • Please post your code instead or in addition to description of the code. – Alexei Levenkov Jul 17 '14 at 15:44
  • Ok, I added in my code. – TheBlueMan Jul 17 '14 at 15:50
  • It is indeed correct way to copy stream (could be `using` instead of `.Close`). Your problem is likely in understanding what API returns - use Fiddler to see what is in result of read+id call. Side note: your `if` don't match, but I don't think it causes any problem in this case. – Alexei Levenkov Jul 17 '14 at 16:10
  • Yep, that was the problem. I was expecting a Tif to return as according to the documentation, but instead it returned a zip of the tif. I never knew of fiddler before, thanks for tell me about the program. – TheBlueMan Jul 17 '14 at 17:55

1 Answers1

0

The APi was returning a zip file instead of a tif file which is what I was expecting. Changed the file extension to .zip and was able to successfully get the tif in the zip.

TheBlueMan
  • 316
  • 1
  • 4
  • 27