I need to read an excel file sended to the controller via ajax (format httppostfilebase) and get their data The file gonna be uploaded from a website and need to not be saved on disk. I got this on PageWeb (The button to select the file and the one who make the upload ("Analizar"):
<input type="file" accept=".xls,.xlsx" id="cargarArchivo" class="cargarArchivo"/>
<br/>
<input type="button" class="analizarArchivo" value="Analizar" onclick="AnalizarArchivoEmpleados()"/>
This on JS (The one who send the file, on format "HttpPostFileBase" (the rest is just validation and that):
var mensajeDialogo;
function AnalizarArchivoEmpleados () {
var nombreArchivo = $(".cargarArchivo").val();
//Se verifica que hay un archivo seleccionado y que su extension sea Excel (xls, xlsx)
if ($(".cargarArchivo").get(0).files.length == 0 || nombreArchivo.indexOf(".xls") == -1) {
mensajeDialogo = "Porfavor seleccione un archivo";
if ($(".cargarArchivo").get(0).files.length != 0 && nombreArchivo.indexOf(".xls") == -1) {
mensajeDialogo = mensajeDialogo + "<br> con extencion valida Excel (xls, xlsx)";
}
$('<div>' + mensajeDialogo + "</div>").dialog({
scriptCharset: "utf-8",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
draggable: false,
modal: true,
resizable: false,
width: 'auto',
title: 'Analizar Archivo',
buttons: {
"Aceptar": function () {
$(this).dialog("close");
}
}
});
} else {
//Se verifica que el navegador soporte windows.FormData , para el envio de archivo excel
if (window.FormData !== undefined) {
alert("si lo soporte oe");
var archivoExcelData = new FormData();
var totalFiles = document.getElementById("cargarArchivo").files.length;
for (var i = 0; i < totalFiles; i++) {
var file = document.getElementById("cargarArchivo").files[i];
archivoExcelData.append("cargarArchivo", file);
}
$.ajax({
type: 'POST',
url: "/Empleados/AnalisisArchivoExcel",
data: archivoExcelData ,
dataType: 'json',
contentType: false,
processData: false,
statusCode: {
401: function () {
MostrarMensajeSinPermiso();
},
200: function (data, status, xhr) {
var expirado = xhr.getResponseHeader('Expires');
if (expirado == "-1") {
MostrarMensajeSessionExpirada();
} else {
alert("volvi");
}
}
}
});
}else {
mensajeDialogo = "Su navegador no soporta envio de archivos <br>Porfavor actualize su navegador";
$('<div>' + mensajeDialogo + "</div>").dialog({
scriptCharset: "utf-8",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
draggable: false,
modal: true,
resizable: false,
width: 'auto',
title: 'Error',
buttons: {
"Aceptar": function () {
$(this).dialog("close");
}
}
});
}
}};
And this on the Controller:
[HttpPost]
public void AnalisisArchivoExcel()
{
for (int i = 0; i < Request.Files.Count; i++)
{
var file = Request.Files[i];
var fileName = Path.GetFileName(file.FileName);
string fileContentType = file.ContentType;
byte[] fileBytes = new byte[file.ContentLength];
var data = file.InputStream.Read(fileBytes, 0,
Convert.ToInt32(file.ContentLength));
}
}
the "data" var, just show numbers and nothing else, i need to read that file a get the data