jQuery ajax文件下载 [英] jquery ajax file download

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

问题描述



我正在从jquery ajax进行C#函数调用,重新调用文件下载选项.

如果我正在使用$.ajax,则无法正常工作,如果我正在提供window.location = url,则该下载框即将到来.

我正在尝试下载一个Excel文件.

我给了这个content type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.
我正在使C#响应对象属性OutputStream写入memorystream对象.

请让我知道是否错过了任何内容.

Hi,

From Jquery ajax i am making a C# function call which is returing a file download option.

If i am using $.ajax, it is not working, if i am giving window.location = url then that download box is coming.

I am trying to download one excel file.

I have given this content type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.
I am making that C# response object property OutputStream write to memorystream object.

Please let me know if i missed anything.

推荐答案

.ajax ,它不起作用,如果我给出了window.location = url,则该下载框为未来.

我正在尝试下载一个Excel文件.

我给了这个content type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.
我正在使C#响应对象属性OutputStream写入memorystream对象.

请让我知道是否错过了任何内容.
.ajax, it is not working, if i am giving window.location = url then that download box is coming.

I am trying to download one excel file.

I have given this content type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.
I am making that C# response object property OutputStream write to memorystream object.

Please let me know if i missed anything.


如果我正确理解了您的问题,则实际上无法直接使用Ajax执行文件下载.您可以使用Ajax请求文件,但您的Ajax响应将包含文件的实际位.由于JavaScript的安全性限制,您将无法使用文件下载对话框将这些位保存到用户的计算机上.有关为什么这样做的更多信息,请参阅我的博客文章查询文件下载插件(演示)可解决此问题并提供其他一些不错的功能.基本上,它为您提供了文件下载(甚至带有回调)的完全Ajax式"体验,这通常是文件下载无法实现的.它还具有其他一些方便的功能:

-无论是成功还是存在错误,用户都不会离开他们启动文件下载的同一页面
-successCallback和failCallback函数可让您明确说明两种情况下的UI行为
-与jQuery UI结合,开发人员可以轻松地向用户显示模式文件,告知用户正在下载文件,可以在下载开始后解散该模式,甚至可以以友好的方式通知用户发生了错误.有关更多示例,请参见演示.
If I understand your question correctly you can''t actually perform a file download with Ajax directly. You can request a file using Ajax but your Ajax response will contain the actual bits for the file. Due to JavaScript security limitations you won''t be able to save those bits onto the user''s computer with a file download dialog. For more information on why this is take a look at my blog post here

I created the Query File Download plugin (Demo) which fixes the problem and provides some other nice features. It basically gives you a "full Ajax-like" experience for file downloads (complete with callbacks even) that isn''t normally possible for file downloads. It also has some other handy features:

- User never leaves the same page they initiated a file download from whether it is successful or there is an error
- successCallback and failCallback functions allow for you to be explicit about what the UI behavior is in either situation
- In conjunction with jQuery UI a developer can easily show a modal telling the user that a file download is occurring, disband the modal after the download starts or even inform the user in a friendly manner that an error has occurred. See the Demo for more example.


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

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