简体   繁体   English

POST 文件列表到 ajax 帖子中的 controller

[英]POST file list to controller in ajax post

I tried to upload documents but I couldnt pass to list my controllers parameter.我试图上传文件,但我无法通过列出我的控制器参数。 My scenario is:我的情况是:

  1. user click to "choose file" button and pick the files and press done用户单击“选择文件”按钮并选择文件并按完成
  2. then my some functions get file list and pass to controller for save locally via POST merhod like below:然后我的一些函数获取文件列表并传递给 controller 以通过 POST merhod 在本地保存,如下所示:

view side: (get file list)查看端:(获取文件列表)

   function saveDocuments(documentList) {
        if (documentList.length > 0)
        {
            var formList = new Array;
            for (var i = 0; i < documentList.length; i++) {
                var form = new FormData();
                var file = documentList[i];

                form.append('FormFile', file);
                formList.push(form);
                }
                 savePhysicalFile(formList);
              }
         }

view side: (post file list)查看端:(发布文件列表)

 function savePhysicalFile(formData)
    {
        if (formData != null)
        {
            $.ajax({
                url: "Installation/SavePhysicalPath",
                type: 'POST',
                dataType: "json",
                contentType: "multipart/form-data",
                data:formData,
                processData: false,
                contentType: false,
                success: function (result) {
                    console.log("Success", result);
                },
                error: function (data) {
                    console.log(data);
                }
            });
        }
    }

In my controller side;在我的 controller 这边; the parameter "model" is always null.参数“型号”始终为 null。 I couldnt pass view side list here.我不能在这里通过查看侧列表。 How can I figure out?我怎么知道?

controller side controller侧

public JsonResult SavePhysicalPath([FromForm] List<FileModel> model)
        {
            var savingRootPath = @"C:\MyDocuments";

            //I'm doing save locally
           
            return Json(savingRootPath);
        }

model side model侧

    public class FileModel
    {
        public string Files { get; set; }

        public IFormFile FormFile { get; set; }
    }

From your code,you may pay attention to two things here:从您的代码中,您可能会在这里注意两件事:

1.For each property of the complex type, model binding looks through the sources for the name pattern prefix.property_name . 1.对于复杂类型的每个属性,model 绑定在源中查找名称模式prefix.property_name If nothing is found, it looks for just property_name without the prefix .For model you receive in backend is a List,you need give the name like: [index].FormFile or model[index].FormFile .如果没有找到,它只查找不带prefixproperty_name 。对于model您在后端收到的是一个列表,您需要给出如下名称: [index].FormFilemodel[index].FormFile

2.Your model has a IFormFile and your action receives a list model,if you only pass the IFormFile you need remove FromForm attribute and be sure do not have [ApiController] .It is a known github issue and this has been moved to Next sprint planning milestone. 2.Your model has a IFormFile and your action receives a list model,if you only pass the IFormFile you need remove FromForm attribute and be sure do not have [ApiController] is a known github issue and this has been moved to Next sprint planning里程碑。

Here is a whole working demo:这是一个完整的工作演示:

View:看法:

<input type="file" multiple onchange="saveDocuments(this.files)"/>
<div class="form-group">
    <input type="button" value="Submit" id="submit" class="btn btn-primary" />
</div>

@section Scripts
{
    <script>
        function saveDocuments(documentList) {
            if (documentList.length > 0) {
                var form = new FormData();
                for (var i = 0; i < documentList.length; i++) {                    
                    var file = documentList[i];                    
                    //change here
                    form.append('model['+i+'].FormFile', file);
                }
                savePhysicalFile(form);
            }
        }    
        function savePhysicalFile(formData) {
            if (formData != null) {
                $.ajax({
                    url: "/Installation/SavePhysicalPath",
                    type: 'POST',
                    dataType: "json",
                    contentType: "multipart/form-data",
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function (result) {
                        console.log("Success", result);
                    },
                    error: function (data) {
                        console.log(data);
                    }
                });
            }
        }
    </script>
}

Controller: Controller:

[HttpPost]
public JsonResult SavePhysicalPath(List<FileModel> model)
{
    var savingRootPath = @"C:\MyDocuments";

    //I'm doing save locally

    return Json(savingRootPath);
}

Result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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