HANDELING一个ASP.NET MVC FileResult返回在(jQuery的)Ajax调用 [英] Handeling an ASP.NET MVC FileResult returned in an (jQuery) Ajax call

查看:1843
本文介绍了HANDELING一个ASP.NET MVC FileResult返回在(jQuery的)Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:
我不想让我的用户下载在我的网页文件。我希望有一个新窗口中打开该文件进行显示或下载那里。

我的实现:
此文件然而,首先必须在服务器端,这可能需要一段时间产生的。当用户点击该按钮下载文件,我做一个Ajax调用,并显示等待动画,直到我得到回应。该控制器操作处理呼叫将生成文件(PDF),并返回一个FileResult。现在在我的Ajax调用回在JavaScript中更迭的功能,我得到的文件数据。

问题:我不知道我应该如何处理这些数据得到它的用户。

解决方法:
现在,我用一种变通方法,我不返回Ajax调用该文件,但它存储在会话。在更迭功能我的window.open(/控制器/ getPDFFromSession)将下载的文件。不过,我preFER不使用会话这几样东西。

在此先感谢。

解决方案
  

问题:我不知道我应该如何处理这些数据得到它的用户。

您不应该使用AJAX来下载文件,因为这个原因。你可以做到以下几点:

  1. 在用户点击下载按钮
  2. 使用JavaScript你表现出一定的进步形象,通知他将不得不等待
  3. 使用JavaScript生成和隐藏的iframe注入到DOM有其的src 属性指向自己生成该文件的控制器动作
  4. 当iframe中加载,你可以隐藏进度图片

Goal:
I want to let my users download a file on my webpage. I want a new window to open and the file to be either displayed or downloaded there.

My implementation:
This file however, first has to be generated on the server-side, which might take a while. When the user clicks the button to download the file, I do an ajax call and show a waiting animation until I get a response. The controller action that handles the call will generate the file (PDF) and return a FileResult. Now in the succes function of my ajax call back in javascript, I get the file data.

Problem: I have no Idea what I'm supposed to do with this data to get it to the user.

Workaround:
Right now I use a workaround where I do not return the file in the ajax call, but store it in session. In the succes function I do window.open("/controller/getPDFFromSession") which will download the file. However, I prefer not to use the session for these kind of things.

Thanks in advance.

解决方案

Problem: I have no Idea what I'm supposed to do with this data to get it to the user.

You shouldn't use AJAX for downloading files for this reason. You could do the following:

  1. The user clicks on the download button
  2. Using javascript you show some progress image informing him that he will have to wait
  3. Using javascript you generate and inject a hidden iframe into the DOM having its src property pointing to the controller action supposed to generate the file
  4. Once the iframe is loaded you could hide the progress image

这篇关于HANDELING一个ASP.NET MVC FileResult返回在(jQuery的)Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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