首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ASP.Net Framework2.0实现大文件的异步流式传输

使用ASP.Net Framework2.0实现大文件的异步流式传输
EN

Stack Overflow用户
提问于 2017-05-27 17:26:12
回答 1查看 991关注 0票数 1

我正在开发一个ASP.NET Framework2.0应用程序。在一个特定的页面上,我提供了一个链接到用户。单击此链接将打开一个窗口,其中包含另一个aspx页面。该页面实际上将http请求发送到指向文件的第三方url (类似于从云中下载文件的镜像url)。使用response.write将http响应发送回第一个页面上的用户,用户从该页面单击链接。

现在,我面临的问题是,如果文件大小很低,那么它就可以正常工作。但是,如果文件很大(即超过1 GB),那么我的应用程序将等待,直到从URL下载完整个文件。我曾尝试使用response.flush()将块数据逐个发送给用户,但用户仍然无法使用应用程序,因为工作进程正忙于从第三方URL获取数据流。

有没有什么方法可以异步下载大文件,以便我的弹出窗口完成执行(下载将正在进行中),并且用户可以在应用程序上并行执行其他活动。

谢谢,苏沃迪普

EN

回答 1

Stack Overflow用户

发布于 2017-05-27 18:05:45

使用WebClient读取远程文件。您可以从WebClient获取流,而不是下载。将其放入while()循环中,并将来自WebClient流的字节推送到响应流中。通过这种方式,您将同时进行异步下载和上传。

HttpRequest示例:

代码语言:javascript
复制
private void WriteFileInDownloadDirectly()
{
    //Create a stream for the file
    Stream stream = null;

    //This controls how many bytes to read at a time and send to the client
    int bytesToRead = 10000;

    // Buffer to read bytes in chunk size specified above
    byte[] buffer = new byte[bytesToRead];

    // The number of bytes read
    try
    {
        //Create a WebRequest to get the file
        HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create("Remote File URL");

        //Create a response for this request
        HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();

        if (fileReq.ContentLength > 0)
            fileResp.ContentLength = fileReq.ContentLength;

        //Get the Stream returned from the response
        stream = fileResp.GetResponseStream();

        // prepare the response to the client. resp is the client Response
        var resp = HttpContext.Current.Response;

        //Indicate the type of data being sent
        resp.ContentType = "application/octet-stream";

        //Name the file 
        resp.AddHeader("Content-Disposition", $"attachment; filename=\"{ Path.GetFileName("Local File Path - can be fake") }\"");
        resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());

        int length;
        do
        {
            // Verify that the client is connected.
            if (resp.IsClientConnected)
            {
                // Read data into the buffer.
                length = stream.Read(buffer, 0, bytesToRead);

                // and write it out to the response's output stream
                resp.OutputStream.Write(buffer, 0, length);

                // Flush the data
                resp.Flush();

                //Clear the buffer
                buffer = new byte[bytesToRead];
            }
            else
            {
                // cancel the download if client has disconnected
                length = -1;
            }
        } while (length > 0); //Repeat until no data is read
    }
    finally
    {
        if (stream != null)
        {
            //Close the input stream
            stream.Close();
        }
    }
}

WebClient流读取:

代码语言:javascript
复制
using (WebClient client = new WebClient())
{
     Stream largeFileStream = client.OpenRead("My Address");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44215183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档