Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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将ObservalArray传递给控制器?_Jquery_Asp.net Mvc_Json_File Upload_Knockout.js - Fatal编程技术网

Jquery 如何使用formdata将ObservalArray传递给控制器?

Jquery 如何使用formdata将ObservalArray传递给控制器?,jquery,asp.net-mvc,json,file-upload,knockout.js,Jquery,Asp.net Mvc,Json,File Upload,Knockout.js,我得到了一个上传控制当我点击上传一个文件浏览后,我需要发送该文件到控制器,我需要发送额外的ObservableArray(其中有相关数据)到正常的控制器 我不知道如何处理这些东西,累了很多很多东西,最后我一无所有 上传按钮上的My Viewmodel代码点击: self.FFAttach = function () { var formdata = new FormData(); //FormData object

我得到了一个上传控制当我点击上传一个文件浏览后,我需要发送该文件到控制器,我需要发送额外的ObservableArray(其中有相关数据)到正常的控制器

我不知道如何处理这些东西,累了很多很多东西,最后我一无所有

上传按钮上的My Viewmodel代码点击:

self.FFAttach = function () {
                    var formdata = new FormData(); //FormData object
                    var fileInput = document.getElementById('FFtoSP');

                    ////Iterating through each files selected in fileInput

                    for (i = 0; i < fileInput.files.length; i++) {
                        //Appending each file to FormData object
                        formdata.append(fileInput.files[i].name, fileInput.files[i]);
                    }

                    //Creating an XMLHttpRequest and sending
                    var xhr = new XMLHttpRequest();
                    xhr.open('POST', '/WorkCompletion/Upload/');

                    xhr.send(formdata);

                    xhr.onreadystatechange = function () {
                        if (xhr.readyState == 4 && xhr.status == 200) {

                            path = xhr.responseText;
                            alert(xhr.responseText);
                        }
                    }
}
  [HttpPost]
public ActionResult Upload(string id)
{

    string uploadedPath = string.Empty;
    for (int i = 0; i < Request.Files.Count; i++)
    {
        HttpPostedFileBase file = Request.Files[i]; //Uploaded file

        //Use the following properties to get file's name, size and MIMEType
        int fileSize = file.ContentLength;
        string fileName = Path.GetFileName(file.FileName);
        fileName = "Contract" + "_" + "CreateContract" + "_" + fileName;
        string mimeType = file.ContentType;
        var path = Path.Combine(Server.MapPath("~/Content/Upload"), fileName);



        //To save file, use SaveAs method
        file.SaveAs(path); //File will be saved in application root


        byte[] fileData = null;
        using (var binaryReader = new BinaryReader(file.InputStream)) { fileData = binaryReader.ReadBytes(file.ContentLength); }
        uploadedPath = UploadToSharepoint(path, id, fileName, file, fileData); 


    }
    return Json(uploadedPath);

}
self.FFAttach=函数(){
var formdata=new formdata();//formdata对象
var fileInput=document.getElementById('FFtoSP');
////遍历fileInput中选定的每个文件
对于(i=0;i
我的控制器代码:

self.FFAttach = function () {
                    var formdata = new FormData(); //FormData object
                    var fileInput = document.getElementById('FFtoSP');

                    ////Iterating through each files selected in fileInput

                    for (i = 0; i < fileInput.files.length; i++) {
                        //Appending each file to FormData object
                        formdata.append(fileInput.files[i].name, fileInput.files[i]);
                    }

                    //Creating an XMLHttpRequest and sending
                    var xhr = new XMLHttpRequest();
                    xhr.open('POST', '/WorkCompletion/Upload/');

                    xhr.send(formdata);

                    xhr.onreadystatechange = function () {
                        if (xhr.readyState == 4 && xhr.status == 200) {

                            path = xhr.responseText;
                            alert(xhr.responseText);
                        }
                    }
}
  [HttpPost]
public ActionResult Upload(string id)
{

    string uploadedPath = string.Empty;
    for (int i = 0; i < Request.Files.Count; i++)
    {
        HttpPostedFileBase file = Request.Files[i]; //Uploaded file

        //Use the following properties to get file's name, size and MIMEType
        int fileSize = file.ContentLength;
        string fileName = Path.GetFileName(file.FileName);
        fileName = "Contract" + "_" + "CreateContract" + "_" + fileName;
        string mimeType = file.ContentType;
        var path = Path.Combine(Server.MapPath("~/Content/Upload"), fileName);



        //To save file, use SaveAs method
        file.SaveAs(path); //File will be saved in application root


        byte[] fileData = null;
        using (var binaryReader = new BinaryReader(file.InputStream)) { fileData = binaryReader.ReadBytes(file.ContentLength); }
        uploadedPath = UploadToSharepoint(path, id, fileName, file, fileData); 


    }
    return Json(uploadedPath);

}
[HttpPost]
公共操作结果上载(字符串id)
{
string uploadedPath=string.Empty;
对于(int i=0;i
如果我想发送一个文件并进一步保存它,上面的代码可以很好地工作,但是当我尝试发送一个obeservable数组时,没有发生任何事情

我尝试了类似于将oberservable作为参数传递的方法,但它确实无法按预期工作,并且尝试了追加
formdata.append(“myparam”,ko.toJS(self.AttachmentUsableArray())当我保留这段代码时,控制器中的调试器甚至伤心地点击了

我提到的链接您可能会发现很方便:


任何想法都将受到欢迎。

您发送可观测阵列的方式与发送任何其他阵列的方式相同。然而,从网页上看不到它;MVC不是这样工作的。如果您希望它具有可观察的特性,那么您必须使用Javascript自己构建它。可观测阵列可以转换为普通阵列。我在上面的帖子中没有提到我的视图模型和服务器模型功能,实际上我构建了非常酷的MVVM模式。正如您所观察到的,我正在尝试
XMLhttpRequest
,在那里我经常对控制器进行大量ajax调用,并传递我的observearray,这很好。任何使用ajax调用或其他方法解决mate的问题。