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
Any ideas are appreciated .