1

我在数据库服务器上有一个大的 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
}

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

4

2 回答 2

0

You can use AJAX which is Asynchronous. For that use a library like JqueryUI and a plugin, take a look a this example

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

Im sure this will do! :D

于 2013-05-09T00:02:41.433 回答
0

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

默认.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>

进程.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();
        }
于 2013-05-09T17:49:29.940 回答