【问题标题】:can I send a list of objects including images using FormData object from view to the controller by ajax call?我可以使用 FormData 对象通过 ajax 调用将包含图像的对象列表从视图发送到控制器吗?
【发布时间】:2023-02-21 23:49:41
【问题描述】:

我有一个包含图像文件的表单列表,并使用 jquery 收集它们并尝试将它们发送到控制器,我尝试发送一个包含图像的表单,并且工作正常,但是当我发送列表时,不起作用

【问题讨论】:

  • 你需要更具体。至少显示收集表单数据并发送它的代码。如果可能的话,在一个可重现的例子中或者至少在一个缩短的版本中,这样我们就不必挖掘不必要的代码。

标签: c# jquery ajax asp.net-core-mvc


【解决方案1】:

是的,您可以使用 FormData 对象通过 AJAX 调用将包含图像的对象列表从视图发送到 controller

为此,您可以创建 FormData 对象的新实例,将列表中的每个表单附加到 FormData 对象,然后通过 AJAX 调用将 FormData 对象发送到 controller

这是一个演示如何执行此操作的示例:

// Create a new instance of the FormData object
var formData = new FormData();

// Iterate over each form in the list
$('form').each(function() {

  // Append the form data to the FormData object
  var form = $(this)[0];
  var formdata = new FormData(form);
  
  formData.append('forms[]', formdata);
});

// Send the FormData object through an AJAX call to the controller
$.ajax({
  url: 'your_controller_url',
  type: 'POST',
  data: formData,
  contentType: false,
  processData: false,
  success: function(response) {
    // Handle the response from the controller
  },
  error: function(error) {
    // Handle any errors that occur during the AJAX call
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多