DUVIDA AJAX - POST ASP NET MVC
Boa tarde! Uma dúvida em AJAX. Eu gostaria de passar um arquivo que foi feito upload na VIEW, um ID e um MENSAGEM para um método Asp net MVC, um ActionResult. Alguém pode me da um exemplo de como fazer: Estou fazendo assim e não funciona:
$(document).ready(function () {
$(''''#btnUpload'''').click(function () {
var fileUploadUrl = $(''''#FileUploadUrl'''').val();
var files = new FormData();
var file1 = document.getElementById("fileOne").files[0];
var file2 = document.getElementById("fileTwo").files[0];
files.append(''''files[0]'''', file1);
files.append(''''files[1]'''', file2);
var data = {
CHAMADO_ID: 1,
MENSAGEM: ''''MIMI I LOVE YOU'''',
FileA: files
};
$.ajax({
type: ''''POST'''',
url: fileUploadUrl,
data: { CHAMADO_ID: CHAMADO_ID, MENSAGEM: MENSAGEM },
dataType: ''''json'''',
cache: false,
contentType: false,
processData: false,
success: function (response) {
$(''''#uploadMsg'''').text(''''Files have been uploaded successfully'''');
},
error: function (error) {
$(''''#uploadMsg'''').text(''''Error has occured. Upload is failed'''');
}
});
});
});
CONTROLLER:
public ActionResult UploadFiles(string MENSAGEM, string CHAMADO_ID)
{
var chamado = CHAMADO_ID;
var mensagemTxt = MENSAGEM;
bool isSuccess = false;
string serverMessage = string.Empty;
var fileOne = Request.Files[0] as HttpPostedFileBase;
var fileTwo = Request.Files[1] as HttpPostedFileBase;
string uploadPath = ConfigurationManager.AppSettings["UPLOAD_PATH"].ToString();
string newFileOne = Path.Combine(uploadPath, fileOne.FileName);
string newFileTwo = Path.Combine(uploadPath, fileTwo.FileName);
fileOne.SaveAs(newFileOne);
fileTwo.SaveAs(newFileTwo);
if (System.IO.File.Exists(newFileOne) && System.IO.File.Exists(newFileTwo))
{
isSuccess = true;
serverMessage = "Files have been uploaded successfully";
}
else
{
isSuccess = false;
serverMessage = "Files upload is failed. Please try again.";
}
return Json(new { IsSucccess = isSuccess, ServerMessage = serverMessage }, JsonRequestBehavior.AllowGet);
}
$(document).ready(function () {
$(''''#btnUpload'''').click(function () {
var fileUploadUrl = $(''''#FileUploadUrl'''').val();
var files = new FormData();
var file1 = document.getElementById("fileOne").files[0];
var file2 = document.getElementById("fileTwo").files[0];
files.append(''''files[0]'''', file1);
files.append(''''files[1]'''', file2);
var data = {
CHAMADO_ID: 1,
MENSAGEM: ''''MIMI I LOVE YOU'''',
FileA: files
};
$.ajax({
type: ''''POST'''',
url: fileUploadUrl,
data: { CHAMADO_ID: CHAMADO_ID, MENSAGEM: MENSAGEM },
dataType: ''''json'''',
cache: false,
contentType: false,
processData: false,
success: function (response) {
$(''''#uploadMsg'''').text(''''Files have been uploaded successfully'''');
},
error: function (error) {
$(''''#uploadMsg'''').text(''''Error has occured. Upload is failed'''');
}
});
});
});
CONTROLLER:
public ActionResult UploadFiles(string MENSAGEM, string CHAMADO_ID)
{
var chamado = CHAMADO_ID;
var mensagemTxt = MENSAGEM;
bool isSuccess = false;
string serverMessage = string.Empty;
var fileOne = Request.Files[0] as HttpPostedFileBase;
var fileTwo = Request.Files[1] as HttpPostedFileBase;
string uploadPath = ConfigurationManager.AppSettings["UPLOAD_PATH"].ToString();
string newFileOne = Path.Combine(uploadPath, fileOne.FileName);
string newFileTwo = Path.Combine(uploadPath, fileTwo.FileName);
fileOne.SaveAs(newFileOne);
fileTwo.SaveAs(newFileTwo);
if (System.IO.File.Exists(newFileOne) && System.IO.File.Exists(newFileTwo))
{
isSuccess = true;
serverMessage = "Files have been uploaded successfully";
}
else
{
isSuccess = false;
serverMessage = "Files upload is failed. Please try again.";
}
return Json(new { IsSucccess = isSuccess, ServerMessage = serverMessage }, JsonRequestBehavior.AllowGet);
}
Rebeca Teles
Curtidas 0
Respostas
Stella Oliveira
11/06/2018
Ei Rebeca, boa tarde.
Acredita que ACABEI de fazer isso? rs
Minha View:
Meu ajax ficou assim:
Acredita que ACABEI de fazer isso? rs
Minha View:
<div> <label class="btn-bmg actions bg-color-3 color-1" for="file" data-toggle="tooltip" data-placement="right" title="Faça o upload do arquivo"> <span class="glyphicon glyphicon-cloud-upload"></span> Clique aqui para envio da planilha </label> @Html.TextBox("postedFile", "", new { id = "file", type = "file", accept = ".csv", @style = "display: none" }) </div>
Meu ajax ficou assim:
$('#file').change(function (event) { var postedFile = new FormData(); var file = document.getElementById('file').files[0]; postedFile.set("postedFile", file); $.ajax({ url: "/Controller/ActionName/", //Nome da Controller e da Action (Rota) data: postedFile, processData: false, contentType: false, type: 'POST', success: function (data) { console.log("reaload"); document.location.reload(true); }, error: function (data) { console.log("Erro ao fazer upload de arquivo"); } }); });
GOSTEI 0