在ASP.NET MVC和JQuery形式插件检测IsAjaxRequest()/文件上传 [英] Detecting IsAjaxRequest() with ASP.NET MVC and JQuery Form Plugin / File Upload

查看:198
本文介绍了在ASP.NET MVC和JQuery形式插件检测IsAjaxRequest()/文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery插件表格上做一个ASP.NET MVC应用程序的文件上传。我已经学会了,因为一个iframe用于文件上传(而不是XMLHtt prequest,这是不可能的),服务器端的支票IsAjaxRequest失败。

I'm using the JQuery Form plugin to do a file upload on an ASP.NET MVC application. I've learned that since an iframe is used for file uploads (rather than XMLHttpRequest, which isn't possible), the server-side check for IsAjaxRequest fails.

我已经看到了有关这个问题的几个职位,但在任何好的解决办法还没有来解决此问题。正如我的应用程序的其余部分,我想能够同时支持启用JavaScript,JavaScript的残疾情况下,这就是为什么我想检测请求是否为AJAX与否。

I've seen a few posts related to this question but haven't come across any good solutions to work around this issue. As with the rest of my application, I'd like to be able to support both JavaScript enabled and JavaScript disabled scenarios, which is why I'd like to detect whether a request is ajax or not.

我认识到,所使用的iframe的做法是不是技术上AJAX,但我试图模仿一个Ajax效果。

I realize that the iframe approach being used is not technically ajax, but I'm trying to mimic an ajax effect.

任何建议将受到欢迎。

推荐答案

我才意识到我完全没有回答这个问题,所以我加入顶端这里,并留下低于我的老回答:

I just realized I totally did not answer the question, so I am adding to the top here, and leaving my old answer below:

问题是在一个iFrame发布的文件时,在X-要求,随着头没有设置,你不能在Javascript正常的表单POST设置特定的请求头。你必须求助于其他的招数,如发送你的文章包含值的隐藏字段,然后更改或覆盖IsAjaxRequest扩展方法也检查此条件。
如何覆盖现有的扩展方法?

The problem is when posting a file over an iFrame, the "X-Requested-With" header is not set and you cannot set specific request headers for a normal form POST in Javascript. You'll have to resort to other tricks, like sending a hidden field with your POST that contains a value, and then change or override the "IsAjaxRequest" extension method to also check for this condition. How to override an existing extension method?

也许是最好的选择很可能会包括您自己的扩展方法,用不同的名称,基于该默认MVC扩展方法code的变化来检测你的iFrame上传帖子,然后任何地方使用您的扩展方法你期望需要它。

Probably the best option would probably be to include your own extension method with a different name, based off the default MVC extension method code with the changes to detect your iFrame upload POST, and then use your extension method anywhere where you expect to need it.


jQuery的实际上是设置默认的X-要求,用'头'XMLHtt prequest。你小心做长达十分有用所有的AJAX调用了jQuery的。

jQuery actually sets the 'X-Requested-With' header to 'XMLHttpRequest' by default. It is quite useful as long as you are careful to do all your AJAX calls over jQuery.

根据您的需要,很容易安装在一个动作过滤器检测到使用它需要的地方,甚至建立成一个控制器类,像这样:

Depending on your needs, it's easy to setup the detection in an action filter to use it where needed, or even build it into a controller class like so:

[jQueryPartial]
public abstract class MyController : Controller
{
   public bool IsAjaxRequest { get; set; }
}

该ActionFilterAttribute:

The ActionFilterAttribute:

public class jQueryPartial : ActionFilterAttribute  
{  
    public override void OnActionExecuting(ActionExecutingContext filterContext)  
    { 
        // Verify if a XMLHttpRequest is fired.  
        // This can be done by checking the X-Requested-With  
        // HTTP header.  
        MyController myController = filterContext.Controller as MyController;
        if (myController != null)
        {
            if (filterContext.HttpContext.Request.Headers["X-Requested-With"] != null
                && filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
            {
                myController.IsAjaxRequest = true;
            }
            else
            {
                myController.IsAjaxRequest = false;
            }
        }
    }
}

和使用实施

public class SomeController : MyController
{
    public ActionResult Index()
    {
          if (IsAjaxRequest)
               DoThis();
          else
               DoThat();

          return View();
    }
}

这篇关于在ASP.NET MVC和JQuery形式插件检测IsAjaxRequest()/文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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