0
Good morning guys,
I’m having a little problem, I imagine it’s simple, but I can’t seem to solve it.
Next, I have to upload a profile photo of the user, the code is working beauty:
JAVASCRIPT
$(".change-photograph").change(function (event) {
var fileName = $(this).val();
var ext = fileName.substring(fileName.lastIndexOf(".") + 1);
if (ext == "gif" || ext == "jpg" || ext == "jpeg" || ext == "png") {
var reader = new FileReader();
$(reader).load(function (event) {
$(".img-photograph").attr("src", event.target.result);
});
reader.readAsDataURL(event.target.files[0]);
$("#save-photograph").click();
} else {
dialog("Alerta", "O arquivo selecionado é inválido.")
}
});
$("#uploadPhotograph").submit(function () {
var formData = new FormData($("#uploadPhotograph")[0]);
$.ajax({
type: "post",
url: "/User/UploadPhotograph",
ajaxasync: true,
data: formData,
success: function () {
addMensagem("Foto alterada com sucesso.");
},
error: function (data) {
alert(data.x);
}
});
return false;
});
function addMensagem(mensage) {
$("#mensagens").append("<li style='overflow: hidden; margin: 4px 0px; border-radius: 2px; border: 1px solid rgb(124, 221, 119); box-shadow: rgba(0, 0, 0, 0.0980392) 0px 2px 4px; color: darkgreen; width: 310px; cursor: pointer; height: 52px; background-color: rgb(188, 245, 188);' class='close-mensagem animated flipInX'><div class='noty_bar noty_type_success'><div class='noty_message' style='font-size: 13px; line-height: 16px; text-align: center; padding: 10px; width: auto; position: relative;'><span class='noty_text'>" + mensage + "</span></div></div></li>");
}
C#
[HttpPost]
public ActionResult UploadPhotograph(UserViewModel user, HttpPostedFileBase file)
{
if (file != null)
{
var edit = false;
if (user.user_photograph != null)
{
if (System.IO.File.Exists(Server.MapPath("~/" + user.user_photograph)))
{
System.IO.File.Delete(Server.MapPath("~/" + user.user_photograph));
}
}
else
{
edit = true;
}
String[] strName = file.FileName.Split('.');
String strExt = strName[strName.Count() - 1];
string pathSave = String.Format("{0}{1}.{2}", Server.MapPath("~/assets/usersPhoto/"), user.Id, strExt);
String pathBase = String.Format("/assets/usersPhoto/{0}.{1}", user.Id, strExt);
file.SaveAs(pathSave);
if (edit)
_userAppService.EditPhotograph(user.Id, pathBase);
}
return new HttpStatusCodeResult(System.Net.HttpStatusCode.OK);
}
My problem is that after he does all this cute stuff he redirects it to this view, he had to stay on the user profile, not redirect it anywhere. Someone knows what’s going on, I have no idea what it might be.
From now on I thank you all!
Could post addMensage() function to analyze?
– Julyano Felipe
Sure, I’ll edit it.
– Samuel Phellip
Try adding preventDefault(); to the Submit event.
– Kayo Bruno
Then I take the false Return?
– Samuel Phellip
If I put preventDefault() does not execute anything from the function.
– Samuel Phellip