在MVC如何调用ashx的通过jquery的处理程序文件 [英] In MVC how to call .ashx handler file through jquery

查看:153
本文介绍了在MVC如何调用ashx的通过jquery的处理程序文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC我需要通过jQuery来调用ashx的处理程序文件。

In mvc i need to call .ashx handler file through jquery.

我试过bleow code

i tried the bleow code

 $("#btnUpload").click(function (evt) {

    var fileUpload = $("#file1").get(0);
    var files = fileUpload.files;

    var data = new FormData();
    for (var i = 0; i < files.length; i++) {
        data.append(files[i].name, files[i]);
    }

    var options = {};
    options.url = "FileUploadHandler.ashx";
    options.type = "POST";
    options.data = data;
    options.contentType = false;
    options.processData = false;
    options.success = function (result) { alert(result); };
    options.error = function (err) { alert(err.statusText); };

    $.ajax(options);

    evt.preventDefault();
});

得到Undifine错误结果
请帮我叫ashx的文件中的MVC ..

getting "Undifine" error
Please help me to call ".ashx" file in MVC..

推荐答案

看看下面这篇文章:

如何调用的HttpHandler通过jQuery的MVC

我的建议也把整个路径到AJAX调用的URL部分在可能的情况以及(如〜/共享/ fileuploadhander.ashx)。

My recommendation is also to put the entire path into the URL part of the AJAX call where possible as well (e.g. "~/shared/fileuploadhander.ashx").

由于蚂蚁普将在上面的问题,MVC正试图将呼叫路由作为一个动作,但后端是哑巴,不知道如何处理.ashx的文件类型(如果我的理解是正确的)。

As Ant P put in the above question, MVC is trying to route the call as an action but the backend is dumb and doesn't know how to handle .ASHX file types (if my understanding is correct).

另外,尽量把事件preventDefault()。在code的顶部。你在本质上说,不停止的单击事件那么你再次调用它的原始功能,然后你终于preventing默认功能(纠正我,如果我错了)。尝试构建更喜欢这个,它会从被解雇两次停止事件:

Also, try putting event.PreventDefault(); at the top of the code. You're in essence saying not to stop the original functionality of the click event then you're calling it again and then you're finally preventing the default functionality (correct me if I am wrong). Try structuring it more like this, it'll stop the event from being fired twice:

$("#btnUpload").click(function (evt) {
  evt.preventDefault();

  var fileUpload = $("#file1").get(0);
  var files = fileUpload.files;

  var data = new FormData();
  for (var i = 0; i < files.length; i++) {
    data.append(files[i].name, files[i]);
  }

  $.ajax({
    url: "FileUploadHandler.ashx",
    type: "POST",
    data: data,
    success: function(data){
       alert(data);
    },
    error: function(err){
       alert(err.statusText);
    }
  });
});

这篇关于在MVC如何调用ashx的通过jquery的处理程序文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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