简体   繁体   中英

posting model from controller to view with ajax

I am trying to post a model from view to controller. I can get other attribute except FileData which is a byte array .though ajax send it to controller but it does not receive it. ,it shows null. How can i get it to controller?

Here is the ajax from View

  $.ajax({
            url: 'ReUploadFile',
            type: 'POST',
            data: JSON.stringify(ChkencodedC),
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                alert(data);
            }
        });

Here is the json data

JSON

 DataContent"36fb1e9e4d5fc3d7ae444fb511e93236"

 FileCollectionId 0

 FileData [100, 106, 102, 26 more...]

 FileName "x.txt"

 Id "00000000-0000-0000-0000-000000000000"

 IsActive true

 Type "text/plain"

 UserId 4

 Version 2

You cannot upload files using pure javascript with AJAX

You may have to use HTML forms as follows,

@using (Html.BeginForm("ACTION", "CONTROLLER", FormMethod.Post, new { EncType="multipart/form-data"})){
  //FORM MARKUP HERE
}

More details can be referred from here,

ASP.NET MVC 4 Web Api ajax file upload

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM