簡體   English   中英

在下載文件時,我收到來自 web API 調用的響應

[英]On downloading a file, I get a response from web API call

在我的代碼中,我正在調用 api 應該用一個文件(excel)回答我,我想將此文件保存在我的電腦中,我嘗試了這里提供的許多解決方案,但似乎沒有什么對我有用

            HttpResponseMessage response = client.PostAsync(url, stringContent).Result;
            if (response.IsSuccessStatusCode)
            {


                string results = response.Content.ReadAsStringAsync().Result;
                byte[] toBeReturned = JsonConvert.DeserializeObject<byte[]>(results);
                log.Info("downloadMasterList FINISH");
                return File(toBeReturned, "application/octet-stream");
            }

收到響應后(假設您收到byte[] ),您需要將其保存到本地驅動器。 您可以使用File.WriteAllBytes來保存。

var saveDir = Path.Combine(Path.GetTempPath(), "some_excel.xlsx");
using (var client = new HttpClient())
{
    var response = client.PostAsync(url, content).Result;
    if (response.IsSuccessStatusCode)
    {
        var content = response.Content.ReadAsByteArrayAsync().Result;
        File.WriteAllBytes(saveDir, content);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM