一个回发后启动文件下载 [英] Start a file download after a Postback

查看:102
本文介绍了一个回发后启动文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我建立了用户一个zip文件下载的部分出口数据库信息。当用户点击一个生成数据按钮,我登录到数据库中的请求创建的zip文件。

In my application I am building a zip file for the User to download for some exported database information. The zip file is created when the user clicks a "generate data" button and I log the request in the database.

在我的网页,我有一个GridView,显示用户的下载历史记录,也为他们提供了下载最新生成的文件的一段时间内的能力。

On my page, I have a Gridview which shows the download history of the User and also offers them the ability to download the latest generated file for a set period of time.

我遇到的问题是,当他们按一下按钮,我想页面刷新(从而刷新GridView和展示他们的最新要求),然后启动文件下载为他们(IE浏览器,弹出提示并让他们决定是否打开/保存/取消)。

The issue I'm having is when they click the button, I'd like the page to refresh (thus refreshing the gridview and showing their latest request) and then start the file download for them (IE, bring up the prompt and let them decide whether to open/save/cancel it).

我能够开始后下载回来,但它开始,因此它不会显示在列表中的最新请求之前我的GridView没有更新。我怎样才能获得GridView控件更新之前下载提示开始?

I'm able to start the download on a post back, but my Gridview is not updating before it begins so it doesn't show the newest request in the list. How can I get the gridview to update BEFORE the download prompt starts?

我用下面开始下载:

    public void BeginDownload()
    {
        FileDownload download = InventoryService.GetLastThreeFileDownloads(this.EmployeeId).First();
        FileInfo fi = new FileInfo(Server.MapPath(SERVER_DOWNLOAD_PATH) + download.DownloadFileName);

        Response.Clear();
        Response.ContentType = "application/zip";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + fi.Name);
        Response.TransmitFile(fi.FullName);
        Response.Flush();
    }

该方法被调用在Page_Load事件作为最后一个项目,如果隐藏字段设置为true(当他们点击按钮来构建文件,我设置)。

The method is called in the Page_Load event as the last item, if a hidden field is set to true (which I set when they click the button to build the file).

我也尝试过的jQuery / AJAX这样调用或者刷新页面,开始下载收效甚微。我曾经想过开一个模态对话框,并让他们点击一个链接开始下载,然后刷新页面时的模式关闭,但这是不得已而为之,如果我不能找到另一种解决方案。

I've also tried doing this through jQuery / AJAX calls to either refresh the page and start the download with little success. I have thought about opening a modal dialog and letting them click a link to start the download and then refresh the page when the modal closes, but that's a last resort if I can't find another solution.

任何帮助是极大AP preciated!

Any help is greatly appreciated!

推荐答案

的事情是,你不发送页面返回给用户。用户点击该按钮,它发送一个HTTP请求,为您生成要下载的包含文件的HTTP响应。如果您想刷新页面后,才送他的文件被下载的,你需要发送一个正常的回发的结果(而不是调用 BeginDownload 法),然后以某种方式强迫他做的其他的要求,在你的文件作出回应。

The thing is you do not send the page back to the user. The user clicks the button, which sends an HTTP request, for which you generate a HTTP response containing the file to be downloaded. If you would like to refresh the page and only after that send him the file to be downloaded, you need to send a normal postback result (not calling the BeginDownload method), and then somehow force him to do another request, upon which you respond with the file.

有各种选项的位置:


  • 元刷新页面上标签

  • 使用一个onload事件的JavaScript

  • 使用iframe

  • ...或让用户点击一个链接,如你所说。

所有方法都有各自的缺点(请尤其注意IE的恶意软件下载的保护,其中,在某些情况下,可恼人的),你可能至少应该包括如果下载没有自动开始下载链接,点击这里。)

All methods have their downsides (be careful especially about IE’s "unwanted download" protection, which, under some circumstances, can be annoying), you should probably at least include a download link with "if the download does not start automatically, click here".)

这篇关于一个回发后启动文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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