I have a file upload button which can upload multiple files. I want to store these files on to a folder using javascript and send these file details from view to the controller. I am doing mvc 4 razor app. Am new to MVC. I know that it can be done with json and ajax post methods. But dont know how to use this.
<script type="text/javascript">
var fileNames = [];
function handleFileUpload(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = document.getElementById('file1').files;
for (var i = 0; files[i]; i++) {
fileNames.push(files[i]);
}
}
$(function () {
$("#btnSubmit").click(function () {
$.post("@Url.Action("FileDetails")", { filename: JSON.stringify(fileNames) }, "json");
});
});
</script>
This was what I have done so far.
I use a jquery plugin called Uploadify
HTML:
<input type="file" id="uploadBtn" />
Javascript:
MVC ACTION:
The url in the javascript method for the parameter 'script' will just be the url to your action. For example if the UploadFile action is in the controller Files then the url will be something like this:
/Files/UploadFile
You can also pass though extra data with 'scriptData' parameter and then just access them the following way