FileUpload with JQuery UI Dialog doesn't working well...
Anybody knows how can I post a html <input type="file"/> by JQuery UI
Dialog?
Is it possible to post a form "enctype="multipart/form-data" with a
"$.post()" method?
I really appreciate to use this method because I can return a Json
result. It's very important for my Architecture....
Let me explain my app:
I'm developing a ASP.Net MVC App and I'm using JQuery Dialog UI to
make a Rich Client app. Well, I have web page that I need to do a
FileUpload, but my code below doesn't working well...
Anybody could help me, please?
Html Code:
<form action="" method="post" id="FormFileUpload" enctype="multipart/
form-data">
<input type="file" id="fileName" name="fileName" />
</form>
Jquery Code:
$(".create").click(function(event) {
event.preventDefault();
$("#dvForm").dialog('open');
})
$("#dvForm").dialog({
bgiframe: true,
autoOpen: false,
resizable: true,
modal: true,
height: 420,
width: 600,
buttons: {
'OK': function() {
PostForm();
},
'Cancelar': function() {
$(this).dialog('close')
}
}
});
function PostForm() {
// Set url Action and Controller asp.net MVC
var _urlAction = '<%= Url.Action
("Create","FileUpload") %>';
var _formData = $("form").serialize();
$.post(_urlAction, _formData, PostFormReturn,
"json");
}
function PostFormReturn(json) {
if (json == true) {
alert("OK");
} else {
alert("Err: " + json.ExceptionApp);
}
}
C# Code:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(string fileName)
{
try
{
thisGetPostedFile();
return Json(true);
}
catch (Exception ex)
{
return Json(new { ExceptionApp = ex.Message });
}
}
private void GetPostedFile()
{
HttpPostedFileBase posted = Request.Files[0];
if (posted.ContentLength > 0)
{
// Add media to file system
posted.SaveAs(HostingEnvironment.MapPath
(Path.GetFileName(posted.FileName));
}
}