1
I have a problem that started to occur for a while. I have a view that sends a FormData
with attachment information (input type file) to a MVC controller via an Ajax. The controller returns a Json with some information. It happens that now overnight is being returned to the view where the ajax is and not the controller’s Json.
The strange thing is that running local works. The problem only occurs when the system is hosted.
Follow below the codes:
View
var fileInput = document.getElementById('files');
var formData = new FormData();
for (i = 0; i < fileInput.files.length; i++) {
formData.append(fileInput.files[i].name, fileInput.files[i]);
}
$.ajax({
type: "POST",
url: "../SistemaRelacionamento/SalvarAnexosRelacionamento",
data: formData,
dataType: 'json',
contentType: false,
processData: false,
success: function (result) {
alert(result);
},
error: function (jqXHR, textStatus, errorThrown) {
alert(" Status: " + textStatus + " Http error:" + errorThrown);
}
});
Controller
[HttpPost]
public ActionResult SalvarAnexosRelacionamento()
{
try
{
List<int> idsAnexos = new List<int>();
// Grava os anexos do relacionamento
for (int i = 0; i < Request.Files.Count; i++)
{
if (!Directory.Exists(Server.MapPath("~/Uploads/AnexoRelacionamentoFolder/")))
Directory.CreateDirectory(Server.MapPath("~/Uploads/AnexoRelacionamentoFolder/"));
var novoNomeArquivo = Guid.NewGuid() + Path.GetExtension(Request.Files[i].FileName);
var savedFileName = "\\GestorUnimed\\Uploads\\AnexoRelacionamentoFolder\\";
savedFileName = Path.Combine(savedFileName, Path.GetFileName(novoNomeArquivo));
var file = Request.Files[i];
file.SaveAs(Server.MapPath("~/Uploads/AnexoRelacionamentoFolder/") +
Path.GetFileName(savedFileName));
var idUltimoAnexo = PegaIdAnexo();
idsAnexos.Add(idUltimoAnexo);
}
return Json(new { success = true, idAnexo = idsAnexos });
}
catch (Exception ex)
{
return Json(new { success = false, idAnexo = 0 });
}
}
Return
I’ve searched everywhere and seen no code to send the FormData
for controller worked. It seems that the FormData
after hosting the system.
Someone can give a light?
Thanks a lot
you have to put all the error in the picture, and you just happened to debug?
– novic
Important you [Dit] your question and explain objectively and punctually the difficulty found, accompanied by a [mcve] of the problem and attempt to solve. To better enjoy the site, understand and avoid closures and negativities worth reading the Stack Overflow Survival Guide in English.
– Bacco