ASP.NET MVC我怎么可以返回从文件下载操作的错误,而无需离开当前页面? [英] ASP.NET MVC how can I return an error from a file download action without leaving the current page?

查看:375
本文介绍了ASP.NET MVC我怎么可以返回从文件下载操作的错误,而无需离开当前页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话从JavaScript文件下载操作:

I am calling a file download action from javascript:

$elem.click(function() { 
    window.location.href = 'MyController/MyFileDownloadAction';
});

控制器返回一个文件:

The controller returns a file:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult MyFileDownloadAction()
{
    /* do some processing, generate an in-memory file... */

    return File(myFileStream, "text/plain", myFileName);
}

这动作有抛出异常(或至少不能够返回文件)的pretty的高的情形产生。

This action has a pretty high likelyhood of throwing an exception (or at least not being able to return the file).

由于应用程序(其中含有大量的动态内容)的性质,我真的不能重定向到一个错误页面在这种情况下。当前页面需要保持开放的莫名其妙。

Due to the nature of the application (which contains a lot of dynamic content), I can't really redirect to an error page in this situation. The current page needs to stay open somehow.

所以我的理想寻找的东西像一个JavaScript弹出,但AFAIK这不会是可能的,因为我不知道任何方式返回javacript指令在非ajaxed控制器调用。如果我显示一个错误页面,我需要强制在新窗口中打开一些如何。是否有任何可能解决这个问题呢?

So I'm ideally looking for something like a javascript pop-up, but afaik this isn't going to be possible since I don't know any way to return a javacript instruction in a non-ajaxed controller call. If I display an error page I need to force it to open in a new window some how. Is there any possible solution to this problem?

推荐答案

我不相信这是可以做到你想要什么。只要你做一个window.location.href ='somelocation',是指要求当前浏览器窗口,要求somelocation'和渲染的结果,无论是什么。因此,你需要在输出结果这一要求的任何错误的信息。

I don't believe it's possible to do what you want. As soon as you do a "window.location.href = 'somelocation'", you are instructing the current browser window to request 'somelocation' and render the result, whatever it may be. Therefore, you need to output whatever error information in the result to that request.

现在,没有任何理由,你会需要重定向到一个错误页面。你可以返回包含错误消息或任何其它类型的结果一个ViewResult

Now, there is no reason that you would need to redirect to an error page. You could return a ViewResult containing the error message or any other kind of result.

这篇关于ASP.NET MVC我怎么可以返回从文件下载操作的错误,而无需离开当前页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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