1

I have an asp.net mvc application and want to upload files and form data with ajax and also want to use [ValidateAntiForgeryToken]-Attribute. But i not want use formData class client side (because of brower support).

My client code:

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);
            }
        });
    }
};

Server code:

    [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");
    }
Xeddon
  • 429
  • 8
  • 18
  • Where is the profileId: id parameter in your action you have only 2 parameters title and text : public JsonResult SendMessage(string title, string text) – Laxman Gite Jul 12 '17 at 08:19
  • i forgot to add "int? profileId,". I edited my code. But this is not the problem. – Xeddon Jul 12 '17 at 08:24
  • Change JsonResult to ActionResult of your action result Name – Laxman Gite Jul 12 '17 at 08:26
  • Ok i changed this. But this is also not the problem. I think problem is something with contentType: false and processData: false. If i comment this (and also "image: files2[0],") the data is sended to the server. If i uncomment contentType: false and processData: false the data is not sended to the server (and also no __RequestVerificationToken for validate). Ok i think this gives me the answer https://stackoverflow.com/questions/2320069/jquery-ajax-file-upload – Xeddon Jul 12 '17 at 08:42

0 Answers0