Mi duda es la siguiente: resulta que estoy pasando un archivo file por ajax
al controlador y lo recibo sin ningún problema.
Pero aparte del file también quiero pasar un parámetro que es un id. que también tengo que recibirlo en el mismo controlador, en el ActionResult
, recibo sin problemas el archivo pero no logro mandar el id.
Alguna idea. es RAZOR
con MVC .Net
Codigo ajax:
function Grabar(id,input_group_) {
console.log("Entré a grabar con " + id + " y " + input_group_);
if (window.FormData !== undefined) {
console.log("comenzare a buscar el archivo");
var fileUpload = $(input_group_).get(0);
var files = fileUpload.files;
var fileData = new FormData();
var id_query = id;
for (var i = 0; i < files.length; i++) {
fileData.append(files[i].name, files[i]);
}
console.log("comenzando carga");
$.ajax({
url: '/Acreditacion_Equipo/Acreditacion_Equipo_Carga',
type: "POST",
contentType: false,
processData: false,
data: {id_query:id_query, fileData},
// data: fileData,id_query,
success: function (result) {
alert(result);
},
error: function (err) {
alert(err.statusText);
}
});
} else {
alert("FormData no esta soportado");
console.log("error no soportado");
}
y en mi controlador C#
recibo bien el archivo pero el string
id lo recibo null
public ActionResult Acreditacion_Equipo_Carga(string id_query)
{
string id_ = id_query;
// Checking no of files injected in Request object
if (Request.Files.Count > 0)
{
try
{
// Get all files from Request object
HttpFileCollectionBase files = Request.Files;
for (int i = 0; i < files.Count; i++)
{
//string path = AppDomain.CurrentDomain.BaseDirectory + "Uploads/";
//string filename = Path.GetFileName(Request.Files[i].FileName);
HttpPostedFileBase file = files[i];
string fname;
// Checking for Internet Explorer
if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
{
string[] testfiles = file.FileName.Split(new char[] { '\\' });
fname = testfiles[testfiles.Length - 1];
}
else
{
fname = file.FileName;
}
// Get the complete folder path and store the file inside it.
fname = Path.Combine(Server.MapPath("~/Uploads/"), fname);
file.SaveAs(fname);
}
// Returns message that successfully uploaded
return Json("File Uploaded Successfully!");
}
catch (Exception ex)
{
return Json("Error occurred. Error details: " + ex.Message);
}
}
else
{
return Json("No files selected.");
}
}
No se porque esta pasando esto. alguna idea de como recibirlo ?