下载并打开一个jQuery的文件 [英] Download and open a file in jQuery

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

问题描述

我公司所经历的.aspx页面下载一个文件,并返回一个文件

I am download a file by going through .aspx page and which returns a file

HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
String Header = "Attachment; Filename=" + sFileName;
HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
FileInfo Dfile = new FileInfo(HttpContext.Current.Server.MapPath(sFilePath));
HttpContext.Current.Response.WriteFile(Dfile.FullName);
HttpContext.Current.Response.End();

这很好。

我想,这样正在下载文件时用户将看到一个GIF微调动画能够通过异步Ajax调用使用jQuery来做到这一点。

I want to be able to do this via async ajax call using jQuery so that while the file is being downloaded user sees a gif spinner animation.

$("#showbusy").fadeIn();
$.ajax({ async : true, type: "GET", url: "download.aspx",
        contentType: "application/text; charset=utf-8",
        success: function (data) {
            $("#showbusy").hide();
        },
        error: function (xmlHttpRequest, textStatus, errorThrown) {
            $("#showbusy").hide();
        }
    });

如果我直接去.aspx页的文件下载,但是这并不是工作做一个AJAX调用此页的某些原因。我可以看到数据在Firebug返回,但一旦完成下载,它只是坐在那里内存

If I go directly to the .aspx page the file downloads, but this is not working by doing an ajax call to this page for some reason. I can see data is being returned in Firebug but once it completes the download it just sits there in memory.

我如何真正触发一个保存文件对话框在浏览器端的文件下载的数据已经被接收后?

How do I actually trigger a save file dialog on the browser side after the file download data has been received?

推荐答案

下载将必须通过保存对话框,以便启动用户才能够把它放在自己的硬盘。

The download will have to be initiated through the save dialog in order for the user to be able to put it on their disk.

如果你的方法是pre取该文件,然后向用户显示一个保存对话框把文件放在自己的硬盘,这是不可行的通过JavaScript。

If your approach is to pre-fetch the file and then show the user a save dialog to put the file somewhere on their disk, this is not doable through javascript.

不过,您的可以的能够使它看起来瞬间为用户做调用下载通过AJAX的文件(你在做什么现在),发送时,提供响应缓存头内容,然后弹出下载对话框(通过一个IFRAME为例)的确切相同的URL一个在AJAX调用。这将让浏览器从它的高速缓存(当AJAX呼叫已完成创建)到用户的磁盘保存文件。

However, you may be able to make it seem instantaneous for the user by doing the call to download the file through ajax (what you're doing now), provide response cache headers when sending the contents, and then popup the download dialog (through an iframe for example) for the exact same URL as the one in the AJAX call. This will get the browser to save the file from its cache (created when the AJAX call was done) to the user's disk.

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

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