I am using ASP.NET MVC3 and I want to have an uploader feature on my project.
I followed this link and yeah, it really worked: File Upload ASP.NET MVC 3.0
But I do want a different approach.
I need to call the uploader from a Jquery and in the jQuery it will call a Controller that will return a result of true or false:
HTML code:
<div id="dialogUpload" title="Upload file" style="display:none;">
{
<input type="file" name="postedFile" class="button"/>
}
</div>
Jquery code:
$("#dialogUpload").dialog({
maxheight: 400,
maxwidth: 400,
resizable: true,
draggable: false,
resizable: false,
modal: true,
buttons: [{
text: "Upload",
click: function () {
$.ajax({
type: "POST",
url: "Controller/UploadFile",
dataType: "json",
success: function (result) {
if (result == true) {
$("#dialogUpload").dialog("close");
ShowAlertMessage("File successfully Uploaded.");
}
else {
$("#dialogUpload").dialog("close");
ShowAlertMessage("Failed to upload the file.");
}
}
});
}
}]
});
Inside my Controller will be this:
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase postedFile)
{
bool uploaded = false;
if (postedFile != null && postedFile.ContentLength > 0)
{
var fileName = Path.GetFileName(postedFile.FileName);
var path = Path.Combine("MYPATH",fileName);
postedFile.SaveAs(path);
uploaded = true;
}
return Content(uploaded);
}
I tried this but it doesn't return to my Jquery so I can't print a message box if it's a success or not. Please help.
Thanks.