Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 使用HttpClient下载ASP.NET MVC大文件_Asp.net Mvc_Httpclient_Large Files - Fatal编程技术网

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

Asp.net mvc 使用HttpClient下载ASP.NET MVC大文件,asp.net-mvc,httpclient,large-files,Asp.net Mvc,Httpclient,Large Files,我参考了很多帖子,仍然无法找到解决方案。我需要从REST端点下载文件,而不会导致内存不足。下面是我用过的代码 public async Task<HttpResponseMessage> DownloadFile() { var client = StorageClientFactory<RestModel>. CreateRESTClient("https://bigfile-test.restsite.net"

我参考了很多帖子,仍然无法找到解决方案。我需要从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;
公共异步任务下载文件()
{
var client=StorageClientFactory。
CreateRESTClient(“https://bigfile-test.restsite.net“”;//创建httpclient对象
var strm=await client.DownloadFileAsResponse(“/300mb.zip”);//使用await-GetStreamAsync(“rest/300mb.zip”)获取流
var res=新的HttpResponseMessage(HttpStatusCode.OK)
{
内容=新的流内容(strm)
};
res.Content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/八位字节流”);
res.Content.Headers.ContentDisposition=新的ContentDispositionHeaderValue(“附件”)
{FileName=“300mb.zip”};
返回res;
HTML


上传
@查看包。结果
下载
当我点击下载按钮时,我得到的是成功响应。但文件没有下载。不知道为什么


状态代码:200,原因短语:“确定”,版本:1.1,内容:System.Net.Http.StreamContent,标题:{Content-Type:application/octet-stream-Content-Disposition:attachment;filename=300mb.zip}

为什么不改用FileStreamResult?@ArneKlein我发现了错误,我应该在WebApi控制器而不是MVC控制器中运行代码。
    <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>