I need your help I'm starting with asp.net mvc, I need to upload files to the server, this is my html:
<form action="javascripts:;" method="post" enctype="multipart/form-data">
<input type="file" id="fileUpload" name="fileUpload" value="" />
<input type="button" id="btn-send-ajax" value="Enviar" />
<a class="btn btn-default btn-sm" name="btn-send-ajax" href="#" onclick="EditPost();" data-toggle="tooltip" title="Exportar"> <i class="dx-icon-save"></i></a>
<script type="text/javascript">
$(document).ready(function () {
$("#btn-send-ajax").click(function () {
var selectFile = ($("#fileUpload"))[0].files[0];
var dataString = new FormData();
if (!selectFile) {
alert("No se ha Cargado El Archivo (html)");
}
dataString.append("fileUpload", selectFile);
$.ajax({
url: '@Url.Action("LoadFileView","Home")',
type: "Post",
data: dataString,
contentType: false,
processData: false,
async:false,
success: function (data) {
if (typeof (data.value) != "undefined") {
alert(data.Message);
} else {
alert("Error No identificado");
}
},
error: function (data) {
}
});
});
});
this is my controller:
public ActionResult LoadFileView(HttpPostedFileBase fileUpload) {
try
{
string path = Server.MapPath("~/Content/Images");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
fileUpload.SaveAs(path + Path.GetFileName(fileUpload.FileName));
}
catch (Exception e)
{
return Json(new { Value = false, Message = e.Message }, JsonRequestBehavior.AllowGet);
}
return Json(new { Value = true, Message = "Subido Con Exito" }, JsonRequestBehavior.AllowGet);
}
everything works fine, it reaches the controller, the line is executed:
return Json(new { Value = true, Message = "Subido Con Exito" }, JsonRequestBehavior.AllowGet);
but in the view it shows me the message: "Unidentified Error" I get to create the folder but not the file. I need a Hand, please ...