0

I got a upload control when i click on upload a file after browsing i need to send that file to controller and with that i need to send additionally a ObservableArray(which has related data) to normal controller .

I'm clueless how to deal this stuff tired lots and lots of stuff and i ended up with nothing cool .

My Viewmodel code on upload button click :

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);
                        }
                    }
}

My controller code :

  [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);

}

The above code works fine if i want to send a file and save it further but nothing is happening for me when i try to send a obeservable array .

I tried something like passing oberservable as parameter and yes it wont work as expected and tried appending formdata.append("myparam", ko.toJS(self.AttachmentUsableArray())); when i keep this piece of code the debugger in controller even hit sadly .

Links i been refering you may find handy :

Webapi ajax formdata upload with extra parameters

File upload Jquery WebApi

Any ideas are appreciated .

2
  • 1
    You send an observable array the same way you would send any other array. It won't be observable from the web page, however; that's not how MVC works. If you want it to have observable characteristics, you have to build that yourself, using Javascript. Observable arrays can be converted to ordinary arrays. Commented Jul 21, 2014 at 13:39
  • point noted mate . i my post above i haven't metioned my view model and server model functions indeed i built pretty cool which works well with MVVM pattern . As you can observe i an trying XMLhttpRequest where i used to do lot of ajax call to controllers and pass my observablearray which works fine . any work around mate using ajax call or whatever . Commented Jul 21, 2014 at 13:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.