English 中文(简体)
Download file using Web API call
原标题:

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

问题回答

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





相关问题
Howto get started with C# 4.0 and .NET 4.0?

I don t want to download Visual Studio 2010. How can I start studying (not developing real applications) C# 4.0 and .NET 4.0 with just a text editor? Can I just download C# 4.0 compiler and .NET 4.0 ...

Mocking Framework with C# 4.0 Support?

Anybody know of a mocking framework that supports C# 4.0? Doesn t matter which one ATM, just need something that will work.

Unit Testing interface contracts in C#

Using the Code Contracts tools available in VS2010 Beta 2, I have defined an interface, a contract class for that interface and two classes that implement the interface. Now when I come to test the ...

How to Iterate Through Array in C# Across Multiple Calls

We have an application where we need to de-serialize some data from one stream into multiple objects. The Data array represents a number of messages of variable length packed together. There are no ...

IronPython ScriptRuntime equivalent to CPython PYTHONPATH

The following import works inside ipy.exe prompt but fails using IronPython ScriptRuntime inside a C# 4.0 program. import ConfigParser C# code: using System; using System.Collections.Generic; using ...

i cant understand the following code

Matrix<float> trainData2 = trainData.GetRows(intVar >> 1, intVar, 1); intVar is integer type... please help me to understand this code.

热门标签