Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp net mvc ajax文件上载,带antiforgerytoken,不带formData_C#_Jquery_Ajax_Asp.net Mvc - Fatal编程技术网

C# asp net mvc ajax文件上载,带antiforgerytoken,不带formData

C# asp net mvc ajax文件上载,带antiforgerytoken,不带formData,c#,jquery,ajax,asp.net-mvc,C#,Jquery,Ajax,Asp.net Mvc,我有一个asp.net mvc应用程序,想用ajax上传文件和表单数据,还想使用[ValidateAntiForgeryToken]-属性。但我不想使用formData类客户端(因为brower支持) 我的客户代码: function sendMessage(id) { if (window.FormData !== undefined) { // var fileData = new FormData(); fileData.append('profil

我有一个asp.net mvc应用程序,想用ajax上传文件和表单数据,还想使用[ValidateAntiForgeryToken]-属性。但我不想使用formData类客户端(因为brower支持)

我的客户代码:

function sendMessage(id) {
    if (window.FormData !== undefined) { //
        var fileData = new FormData();
        fileData.append('profileId', id);
        fileData.append('title', $('#Message_Title').val());
        fileData.append('text', $('#Message_Text').val());
        fileData.append('__RequestVerificationToken', $('[name=__RequestVerificationToken]').val());
        var files = $("#Message_Image").get(0).files;
        if (files[0]) {
            fileData.append(files[0].name, files[0]);
        }
        $.ajax({
            url: '/Manage/SendMessage',
            type: "POST",
            contentType: false,
            processData: false,
            data: fileData,
            success: function (result) {
                alert(result);
            },
            error: function (err) {
                alert(err.statusText);
            }
        });
    } else {
        var files2 = $("#Message_Image").get(0).files;
        $.ajax({
            url: '/Manage/SendMessage',
            type: 'POST',
            //contentType: false,
            //processData: false,
            data: {
                profileId: id,
                title: $('#Message_Title').val(),
                text: $('#Message_Text').val(),
                //image: files2[0], <--- IF I UNCOMMENT THIS IT NOT WORKS ANYMORE
                __RequestVerificationToken: $('[name=__RequestVerificationToken]').val()
            },
            success: function (result) {
            },
            error: function (err) {
                alert(err.statusText);
            }
        });
    }
};

操作中的profileId:id参数在哪里?您只有两个参数title和text:public JsonResult SendMessage(string title,string text)我忘记添加“int?profileId”。我编辑了我的代码。但这不是问题。将您的操作结果名称的JsonResult更改为ActionResult好的,我更改了。但这也不是问题所在。我认为问题在于contentType:false和processData:false。如果我对此进行注释(以及“image:files2[0]”),数据将发送到服务器。如果我取消注释contentType:false和processData:false,则数据不会发送到服务器(也不会发送\uu RequestVerificationToken进行验证)。好的,我想这给了我一个答案,profileId:id参数在您的操作中的位置您只有两个参数title和text:public JsonResult SendMessage(string title,string text)我忘记添加“int?profileId”。我编辑了我的代码。但这不是问题。将您的操作结果名称的JsonResult更改为ActionResult好的,我更改了。但这也不是问题所在。我认为问题在于contentType:false和processData:false。如果我对此进行注释(以及“image:files2[0]”),数据将发送到服务器。如果我取消注释contentType:false和processData:false,则数据不会发送到服务器(也不会发送\uu RequestVerificationToken进行验证)。好的,我想这给了我答案
    [HttpPost]
    [ValidateAntiForgeryToken]
    public JsonResult SendMessage(int? profileId, string title, string text)
    {
        HttpPostedFileBase file = Request.Files["Image"];
        HttpFileCollectionBase files = Request.Files;
        return Json(null, "text/html");
    }