Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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
Jquery 如何将FormData中的附加数据发送到web api_Jquery_Asp.net_Asp.net Mvc 4_Asp.net Web Api_Form Data - Fatal编程技术网

Jquery 如何将FormData中的附加数据发送到web api

Jquery 如何将FormData中的附加数据发送到web api,jquery,asp.net,asp.net-mvc-4,asp.net-web-api,form-data,Jquery,Asp.net,Asp.net Mvc 4,Asp.net Web Api,Form Data,我正在上传文件,为此我在jquery ajax中使用FormData对象并将其传递给ASP.NET MVC WEB API,它工作正常,我能够在服务器端获取文件,但我也希望通过相同的请求传递一些额外的细节 我可以在标题中添加额外的数据,并且我可以在服务器端从标题中获取数据,但我也会对移动应用程序使用相同的API。所以,如果我能将数据作为函数的参数传递,那就太好了 那个么,如何在formData对象中传递额外的数据,以及如何在服务器端获取这些数据呢 我的密码是 function uploadEva

我正在上传文件,为此我在jquery ajax中使用FormData对象并将其传递给ASP.NET MVC WEB API,它工作正常,我能够在服务器端获取文件,但我也希望通过相同的请求传递一些额外的细节

我可以在标题中添加额外的数据,并且我可以在服务器端从标题中获取数据,但我也会对移动应用程序使用相同的API。所以,如果我能将数据作为函数的参数传递,那就太好了

那个么,如何在formData对象中传递额外的数据,以及如何在服务器端获取这些数据呢

我的密码是

function uploadEvaluationFile() {

    var files = $("#file_UploadFile1").get(0).files;
    if (files.length > 0) {
        if (window.FormData !== undefined) {
            var data = new FormData();
            for (var x = 0; x < files.length; x++) {
                data.append("file1" + x, files[x]);
            }


data.append("UserId", 5);
            data.append("ArtCategory", 5);
            data.append("Title", "Title1");
            data.append("Description", "Desc 1");

            $.ajax({
                type: "POST",
                url: '/Home/saveEvaluationFile',
                contentType: false,
                processData: false,
                data: data,
                async: false,
                beforeSend: function (xhr) {
                    xhr.setRequestHeader('identifier', 111);
                    xhr.setRequestHeader('oldFileName', 222);
                },
                dataType: "json",
                success: function (result) {
                    console.log(result);
                },
                error: function (err) {
                    console.log(err);
                }
            });
        } else {
            alert("This browser doesn't support HTML5 file uploads!");
        }
    }
}
你可以用同样的方法

 data.append("file1" + x, files[x]);
例:

获取数据

[HttpPost]
public async Task<JsonResult> saveEvaluationFile(FileModel model)
{
  foreach (string image in model.images)
  {
     HttpPostedFileBase hpf =  model.images[image ] as HttpPostedFileBase;
    // nesasary save part
  }
[HttpPost]
公共异步任务saveEvaluationFile(文件模型)
{
foreach(model.images中的字符串图像)
{
HttpPostedFileBase hpf=model.images[image]作为HttpPostedFileBase;
//nesasary保存部分
}
将文件添加到模型

 public class FileModel
 {
 ...
 public HttpPostedFileBase File1 { get; set; }//one file
 public IEnumerable<HttpPostedFileBase> images {get;set;}//file collection
 }
公共类文件模型
{
...
公共HttpPostedFileBase文件1{get;set;}//一个文件
公共IEnumerable映像{get;set;}//文件集合
}

我无法传递HttpPostedFileBase的可能重复项,因为我没有使用表单标记。在object中,我也可以添加文件吗?不确定您的意思。您可以使用
FormData
传递您想要的任何内容。但如果您只向方法(例如模型)添加参数,您会发现这要容易得多因此,您没有访问值表单
请求
正确,我按照您的建议做了,我也获取了所有数据,但没有获取文件。请查看我的更新代码,因为您已将它们命名为
文件10、
文件11
文件12
等。您只需命名(比如)
文件
然后模型需要是
公共IEnumerabe文件{get;set;}
在模型对象中,我也可以添加文件吗?请检查我的更新问题,我添加了类,我正在获取除文件以外的所有数据。服务器端的file1属性为空。您需要做的第一件事是按照我在代码中的建议将文件属性设置为集合公共IEnumerable images{get;set;}//File collectionLet us。好的解决方案。棒极了!
 formdata.append('Key', 'Value');
[HttpPost]
public async Task<JsonResult> saveEvaluationFile(FileModel model)
{
  foreach (string image in model.images)
  {
     HttpPostedFileBase hpf =  model.images[image ] as HttpPostedFileBase;
    // nesasary save part
  }
 public class FileModel
 {
 ...
 public HttpPostedFileBase File1 { get; set; }//one file
 public IEnumerable<HttpPostedFileBase> images {get;set;}//file collection
 }