3

I am using Web API to download a file. Let me first preface that this is new territory for me. My download function inside the Web API code will initiate a download if I go directly to the API's homepage. However, using the response from the Web API's call from my other webpage I don't know how to retrieve the file. I see a lot of similar questions but none that really have a definitive answer.

This is my code to send the download back to the caller:

    public HttpResponseMessage GetDownloadFile(string uid, string fileID, string IP_ADDRESS)
    {

        MemoryStream ms = null;


        string sDecryptedUserID = uid;
        string sDecryptedFileID = fileID;
        string sDecryptedIPAddress = IP_ADDRESS;

        var downloadRecord = GetDownLoadRecord(sDecryptedUserID, sDecryptedFileID);
        if (downloadRecord != null)
        {
            ms = ExportFile(downloadRecord, sDecryptedUserID, sDecryptedIPAddress);
            if (ms != null)
                UpdateDownloadLog(downloadRecord, sDecryptedUserID, sDecryptedIPAddress);
        }
         //This is where I setup the message.
        if (ms != null)
        {
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            result.Content = new StreamContent(ms);
            result.Content.Headers.ContentType =
                new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
            return result;
        }
        else
            return new HttpResponseMessage(HttpStatusCode.InternalServerError);
    }

To receive the message I use this:

        private bool GetDownload(string[] download, string userid, string IPADDRESS)
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(uri);

        string url = @"api/Download?uid=" + userid + "&fileID=" + download[0] + "&IP_ADDRESS=" + IPADDRESS;

        HttpResponseMessage responseParent = client.GetAsync(url).Result;
        if (responseParent.IsSuccessStatusCode)
        {   

            //I don't know what to set the returned value to.
            StreamContent respMessage = responseParent.Content.ReadAsAsync<StreamContent>().Result;
            var byteArray = respMessage.ReadAsByteArrayAsync();
            //ExportFile(byteArray, download);
            return true;
        }
        else
            return false;

    }

I can't find any information that says how to parse the returned value here. I have plenty of code that returns datasets from the WebAPI but this is throwing me off. If anyone could help I would greatly appreciate it.

I found this JQuery example but I really want to do this in C#. Jquery Web API Example

Community
  • 1
  • 1
cjohns
  • 1,520
  • 2
  • 14
  • 21
  • Is this helpful? http://blogs.msdn.com/b/henrikn/archive/2012/02/17/downloading-a-google-map-to-local-file.aspx. In particular, his `ReadAsFileAsync` method. – Jim Mischel Jul 25 '13 at 21:58

1 Answers1

0

try this:

    HttpResponseMessage responseParent = client.GetAsync(url).Result;
    if (responseParent.IsSuccessStatusCode) {

        var byteArray = responseParent.Content.ReadAsByteArrayAsync().Result;

        //ExportFile(byteArray, download);
        return true;

    } else

        return false;

... or take a look at this: example that creates an extension method for HttpContent to directly save to a file stream. the code looks better like this.

greetings