ASP.NET MVC使用HttpClient下载大文件

时间:2018-06-14 16:05:39

标签: asp.net-mvc httpclient large-files

我提到很多帖子,仍然无法得到解决方案。我需要从REST端点下载文件而不会导致内存不足。下面是代码,我用过

    public async Task<HttpResponseMessage> DownloadFile()
    {

        var client = StorageClientFactory<RestModel>.
            CreateRESTClient("https://bigfile-test.restsite.net");   //creates httpclient object

        var strm = await client.DownloadFileAsResponse("/300mb.zip");   //gets the stream using await GetStreamAsync("rest/300mb.zip")

        var res = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new StreamContent(strm)
        };

        res.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        res.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        { FileName = "300mb.zip" };

        return res;

HTML

    <form enctype="multipart/form-data">
        <input type="file" id="File1" name="file" multiple />
        <button type="submit" formaction="/Default/UploadFile" name="btnUpload" value="UploadFile" formmethod="post">
            Upload
        </button>
        <label>@ViewBag.Result</label>  
        <button type="submit" formaction="/Default/DownloadFile" name="btnDownload" value="DownloadFile">
            Download
        </button>                     
    </form>

当我点击下载按钮时,我得到的是成功回复。但是没有下载文件。不知道为什么。

StatusCode:200,ReasonPhrase:'OK',版本:1.1,内容:System.Net.Http.StreamContent,标题:{Content-Type:application / octet-stream Content-Disposition:attachment; filename = 300mb.zip}

0 个答案:

没有答案
相关问题