如何:保存从Web服务的文件,而不从停止渲染页面 [英] How To: Save a file from a webservice without stopping the page from rendering

查看:191
本文介绍了如何:保存从Web服务的文件,而不从停止渲染页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两页,索引和下载,点击下载时,它从服务检索一个byte []我用Response.BinaryWrite来保存文件的控制器。这样做的问题是,使用Response.Clear当它从呈现停止下载页但成功下载该文件。有什么办法来下载文件,并渲染页面?

我使用.NET 4,ASP,单轨城堡,C#

据我所知,通过使用MVC我能够使用的ActionResult和FileResult作为返回类型我的看法,我不过仅限于使用单轨城堡,由于它是现有的项目我已经采取所有权最近,没有重量改变它的技术,只是还没有。

下面是我的榜样code

 公共类myController的:控制器
{
    公共无效指数()
    {
        的PropertyBag [includeZeroBalances] = FALSE;
        的PropertyBag [TODATE] = DateTime.Today.ToShortDateString();
    }    公共无效下载(布尔includeZeroBalances,日期TODATE)
    {
        MYPROXY代理= GetProxy();
        字节[] =文件proxy.GetFile(includeZeroBalance,TODATE);
        Response.Clear();
        Response.ContentType =应用程序/压缩
        Response.AppendHeader(内容处置,附件;文件名=TestFileName.zip);
        Response.BinaryWrite(文件);
    }
}

下面是索引页

  $ {Form.FormTag({@行动:'下载'})}
    <表>
        &所述; TR>
            < TD> $ {Form.LabelFor(TODATE,结束日期(年/月/日):)}< / TD>
            < TD><输入类型=文本ID =TODATENAME =TODATE值=$ {TODATE?}/>< / TD>
        < / TR>
        &所述; TR>
            < TD> $ {Form.LabelFor(includeZeroBalances,包括零余额)}< / TD>
            &所述; TD> $ {Form.CheckboxField(includeZeroBalances)}&下; / TD>
        < / TR>
        &所述; TR>
            < TD>&安培; NBSP;< / TD>
            < TD> $ {Form.Submit(下载,{@class:submitb'})}< / TD>
        < / TR>
    < /表>
$ {Form.EndFormTag()}

下面是下载页面

 <表>
    &所述; TR>
        < TD>你的文件已经成功&LT下载; / TD>
    < / TR>
< /表>


解决方案

我想出答案并不像一般的,因为我也喜欢,但这种解决方案的功能。

 公共类myController的:控制器
{
    公共无效指数()
    {
        的PropertyBag [includeZeroBalances] = FALSE;
        的PropertyBag [TODATE] = DateTime.Today.ToShortDateString();
    }    公共无效下载(布尔includeZeroBalances,日期TODATE)
    {
        MYPROXY代理= GetProxy();
        字节[] =文件proxy.GetFile(includeZeroBalance,TODATE);
        的PropertyBag [downloadurl] =数据:应用程序/压缩; BASE64,+ System.Convert.ToBase64String(文件);
    }
}

下面是索引页

  $ {Form.FormTag({@行动:'下载'})}
    <表>
        &所述; TR>
            < TD> $ {Form.LabelFor(TODATE,结束日期(年/月/日):)}< / TD>
            < TD><输入类型=文本ID =TODATENAME =TODATE值=$ {TODATE?}/>< / TD>
        < / TR>
        &所述; TR>
            < TD> $ {Form.LabelFor(includeZeroBalances,包括零余额)}< / TD>
            &所述; TD> $ {Form.CheckboxField(includeZeroBalances)}&下; / TD>
        < / TR>
        &所述; TR>
            < TD>&安培; NBSP;< / TD>
            < TD> $ {Form.Submit(下载,{@class:submitb'})}< / TD>
        < / TR>
    < /表>
$ {Form.EndFormTag()}

下面是下载页面

 <表>
    &所述; TR>
        < TD>您的文件已成功生成。 < A HREF =$ {} downloadurl>点击此处< / A>下载你的文件和LT; / TD>
    < / TR>
< /表>

您还可以添加JavaScript来自动下载文件中的一个新窗口。

 的document.ready(函数()
{
    window.open($ {downloadurl});
});

使用这种方法来保存文件的优点:
  没有文件保存在服务器上。
  下载文件大约超过300KB时略低开销
  你不覆盖响应数据,因此视图将呈现按正常。

使用这种方法来保存文件的缺点:
  不是通用的,足以对任何版本的任何浏览器上使用。
  如果用于静态文件,当文件变化时,URI将具有自该文件的内容中嵌入的URI来再次生成

我希望这可以帮助其他人谁似乎一直停留在同样的问题。我见过在多个电路板这样的问题,没有简单的解决方案。

I have a controller with two pages, Index and Download, when clicking download it retrieves a byte[] from the service and I use Response.BinaryWrite to save the file. The problem with this is that when using Response.Clear it stops the Download page from rendering but downloads the file successfully. Is there any way to download the file and render the page?

I'm using .NET 4, ASP, Monorail Castle, C#

I am aware that by using MVC I am able to use ActionResult and FileResult as return types for my views, I am however limited to using Monorail Castle due to it being an existing project I've taken ownership of recently and have no weight in changing it's technology just yet.

Below is my example code

public class MyController : Controller
{
    public void Index()
    {
        PropertyBag["includeZeroBalances"] = false;
        PropertyBag["toDate"] = DateTime.Today.ToShortDateString();
    }

    public void Download(bool includeZeroBalances, DateTime toDate)
    {
        MyProxy proxy = GetProxy();
        byte[] file = proxy.GetFile(includeZeroBalance, toDate);
        Response.Clear();
        Response.ContentType = "application/zip";
        Response.AppendHeader("Content-Disposition", "attachment; filename="TestFileName.zip");
        Response.BinaryWrite(file);
    }
}

Here is the Index page

${Form.FormTag({@action: 'Download'})}
    <table>
        <tr>
            <td>${Form.LabelFor("toDate", "To Date (yyyy/mm/dd):")}</td>
            <td><input type="text" id="toDate" name="toDate" value="${?toDate}" /></td>
        </tr>
        <tr>
            <td>${Form.LabelFor("includeZeroBalances", "Include Zero Balances:")}</td>
            <td>${Form.CheckboxField("includeZeroBalances")}</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>${Form.Submit("Download", {@class: 'submitb'})}</td>
        </tr>
    </table>
${Form.EndFormTag()}

Here is the download page

<table>
    <tr>
        <td>Your file has been downloaded successfully</td>
    </tr>
</table>

解决方案

The answer I came up with is not as generic as I would have liked, yet this solution is functional.

public class MyController : Controller
{
    public void Index()
    {
        PropertyBag["includeZeroBalances"] = false;
        PropertyBag["toDate"] = DateTime.Today.ToShortDateString();
    }

    public void Download(bool includeZeroBalances, DateTime toDate)
    {
        MyProxy proxy = GetProxy();
        byte[] file = proxy.GetFile(includeZeroBalance, toDate);
        PropertyBag["downloadurl"] = "data:application/zip;base64," + System.Convert.ToBase64String(file);
    }
}

Here is the Index page

${Form.FormTag({@action: 'Download'})}
    <table>
        <tr>
            <td>${Form.LabelFor("toDate", "To Date (yyyy/mm/dd):")}</td>
            <td><input type="text" id="toDate" name="toDate" value="${?toDate}" /></td>
        </tr>
        <tr>
            <td>${Form.LabelFor("includeZeroBalances", "Include Zero Balances:")}</td>
            <td>${Form.CheckboxField("includeZeroBalances")}</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>${Form.Submit("Download", {@class: 'submitb'})}</td>
        </tr>
    </table>
${Form.EndFormTag()}

Here is the download page

<table>
    <tr>
        <td>Your file has been generated successfully. <a href="${downloadurl}">Click here</a> to download your file</td>
    </tr>
</table>

You could also add javascript to automate downloading the file in a new window.

document.Ready(function()
{
    window.open("${downloadurl}");
});

Advantages of using this methodology to save files: No files are saved on the server. Slightly lower overhead when downloading files over approximately 300kb You don't overwrite the response data, hence the view will be rendered as per normal.

Disadvantages of using this methodology to save files: Not generic enough to be used on any browser of any version. If used for Static Files, when the file changes, the uri will have to be generated again since the file contents are embedded in the uri

I hope this helps others who seem to have been stuck on the same issue. I've seen this type of question across a number of boards with no straight forward solutions.

这篇关于如何:保存从Web服务的文件,而不从停止渲染页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆