在回发后开始文件下载 [英] Start a file download after a Postback

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

问题描述

在我的应用程序中,我正在构建一个 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?

我正在使用以下内容开始下载:

I'm using the following to start the download:

    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.

非常感谢任何帮助!

推荐答案

问题是您没有将页面发送回给用户.用户单击发送 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.

这里有多种选择:

  • 在页面上包含元刷新标签
  • 使用加载 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天全站免登陆