下载大文件asp.net mvc 3?

时间:2013-05-08 22:00:55

标签: asp.net asp.net-mvc asynchronous download filestream

我在数据库服务器上有一个大型pdf文件(最多2 GB),我想发送给客户端进行下载。另外,我的Web服务器有大小限制,在从数据服务器请求后,无法在其上存储完整的文件。我正在使用asp.net mvc 3.有关如何实现此目的的任何建议?此外,我希望它是异步的,因为我不想阻止用户点击网页上的其他按钮。

我尝试过使用此代码。

//code for getting data from data server into inputstream
HttpContext.Response.ContentType = "application/pdf";
HttpContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
HttpContext.Response.BufferOutput = false;     
try
{
   using (Stream inputStream = resp.GetResponseStream())
   {
     byte[] buffer = new byte[SegmentSize];
     int bytesRead;
     while ((bytesRead = inputStream.Read(buffer, 0, SegmentSize)) > 0)
     {
        HttpContext.Response.OutputStream.Write(buffer, 0, bytesRead);
        HttpContext.Response.Flush();
     }
    }
 }
catch (Exception ex)
{
   //some code
}

这会下载文件,但后来我不知道如何让它异步?还有其他方法可以进行异步下载吗?

2 个答案:

答案 0 :(得分:0)

您可以使用异步的AJAX。为此,使用像JqueryUI和插件这样的库,看看这个例子

http://www.plupload.com/example_custom.php

我相信这样做! :d

答案 1 :(得分:0)

在我的最终应用程序中,我没有使用Web服务,但简单的AJAX和ASPX页面就是我完成它的方式!

Default.ASPX文件

       <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CloudWF._Default"
        Culture="auto" meta:resourcekey="PageResource1" UICulture="auto" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <link type="text/css" href="css/redmond/jquery-ui-1.8.21.custom.css" rel="stylesheet" />

    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="jsEng" runat="server">
                <Scripts>
                    <asp:ScriptReference Path="~/js/jquery-1.7.2.min.js" />
                    <asp:ScriptReference Path="~/js/jquery-ui-1.8.21.custom.min.js" />
                </Scripts>
            </asp:ScriptManager>
    <input type='submit' value='Process New Records' id='trigger' onclick='BeginProcess(); return false;' />
</div>
</form>
</body>
<script type="text/javascript">
    var btnStart;

    $(function () {
        btnStart = $("#trigger").button();
        imgLoad = $("#loader").hide();
    });

    function BeginProcess() {
        btnStart.val("Collecting Data...");
        //to get the value of the radiobuttons
        //alert($('input[name=A]:checked').val()) --> [name=*] where *= whatever the name of the radioset

        // Create an iframe.
        var iframe = document.createElement("iframe");

        // Point the iframe to the location of
        //  the long running process.
        iframe.src = "process.aspx";

        // Make the iframe invisible.
        iframe.style.display = "none";
        // Add the iframe to the DOM.  The process
        //  will begin execution at this point.
        document.body.appendChild(iframe);

        btnStart.val("Processing...");
        imgLoad.show();

        btnStart.button("disable");

    }

    function UpdateProgress(RecordComplete, Message, step) {
        btnStart.val("Downloaded Record " + RecordComplete + Message);
        $("#progressbar").progressbar({
            value: step
        });

        if (step >= 100) {
            imgLoad.hide();
            btnStart.val("Download Complete!");
        }
    }

</script>

Process.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Web;

         public partial class process : System.Web.UI.Page
        {
           WebClient wc = new WebClient();
           Dictionary<string, string> AudioURLs = new Dictionary<string, string>();

        protected void Page_Load(object sender, EventArgs e)
        {
            // Padding to circumvent IE's buffer*
            Response.Write(new string('*', 256));
            Response.Flush();

            ProcessRecords();
        }

public void ProcessRecords()
        {
            int recTotal;
            recordList = (IQueryable<Record>)(Session["UNPROCESSED"]);
            recTotal = recordList.Count();

            if (recordList.Count() > 0)
            {
                foreach (Record record in recordList)
                {
                    try
                    {
                        curRow++;
                        step = ((decimal)curRow / (decimal)recTotal) * 100;

                        CreateDictionary();
                        CreateFolderwithAudios(record.tSessionID);
                        //record.processed = true;

                        db.SubmitChanges();

                        UpdateProgress(curRow, " of " + recTotal, step);
                    }
                    catch (Exception x)
                    {
                        HttpContext.Current.Response.Write("Exception: " + x.Message);
                    }
                }
                Session["UNPROCESSED"] = null;
            }           
        }
public Dictionary<string, string> CreateDictionary()
        {
            AudioURLs.Clear();

            #region Add Values to Dictionary

            return AudioURLs;
        }
public void DownloadAudios(string _subFolder, Dictionary<string, string> _AudioSources)
        {
            foreach (KeyValuePair<string, string> kvp in _AudioSources)
            {
                if (kvp.Value.StartsWith("http://"))
                {
                    try
                    {
                        wc.DownloadFile(kvp.Value, audiosPath + "\\" + _subFolder + "\\" + kvp.Key + ".wav");
                    }
                    catch (WebException webex)
                    {
                        throw new WebException(webex.Message);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);

                    }
                }
            }
        }

 public void CreateFolderwithAudios(string folderName)
        {
            try
            {
                //create the folder where the audios are going to be saved
                Directory.CreateDirectory(audiosPath + folderName);

                //create and read the Dictionary that contains the URLs for the audio files
                DownloadAudios(folderName, AudioURLs);

            }
            catch (AccessViolationException ae)
            {
                HttpContext.Current.Response.Write(ae.Message);
            }
            catch (System.Exception x)
            {
                HttpContext.Current.Response.Write(x.Message);
            }
        }

 protected void UpdateProgress(int PercentComplete, string Message, decimal step)
        {
            // Write out the parent script callback.
            Response.Write(String.Format(
              "<script>parent.UpdateProgress({0}, '{1}',{2});</script>",
              PercentComplete, Message, step));
            // To be sure the response isn't buffered on the server.
            Response.Flush();
        }