3

Web API を使用してファイルをダウンロードしています。最初に、これが私にとって新しい領域であることを前置きさせてください。API のホームページに直接アクセスすると、Web API コード内のダウンロード関数がダウンロードを開始します。ただし、他の Web ページからの Web API の呼び出しからの応答を使用すると、ファイルを取得する方法がわかりません。同様の質問がたくさんありますが、決定的な答えがあるものはありません。

これは、ダウンロードを発信者に送り返すための私のコードです:

    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);
    }

メッセージを受信するには、これを使用します。

        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;

    }

ここで返された値を解析する方法を示す情報が見つかりません。WebAPI からデータセットを返すコードがたくさんありますが、これは私をうんざりさせています。誰かが助けてくれれば、とても感謝しています。

この JQuery の例を見つけましたが、これを C# で行いたいと思っています。Jquery Web API の例

4

1 に答える 1