I have an upload of video type files, which saves them on the server by means of an asynchrony, but at the moment that the video is larger than 5MB it does not do it for me ( It cannot be the limit since it I have assigned to 120MB ), I have located the place of the problem and it is at the moment that I assign to the variable that I have the FormData()
, I leave the code of the operation:
JavaScript
//Funcion asincrona
async function GuardarNuevoDocumento() {
r1 = await GuardarDocumento();
r2 = await GuardarTodo(r1);
}
function GuardarDocumento() {
try {
var tipo = $('#Tipo').val();
var urlDoc = 'FuncionControlador';
var campoDoc = 'idInput';
var data = new FormData();
var files = $("#" + campoDoc).get(0).files;
if (files.length > 0) {
//Si el archivo es mayor a 5 MB data continua siendo un
//FormData vacio, el append no lo realiza
data.append("file", files[0]);
}
var ajaxRequest = $.ajax({
type: "POST",
url: urlDoc,
contentType: false,
processData: false,
data: data,
success: function () {
console.log("fin");
},
error: function(e){
console.log(e);
}
});
} catch(e) {
console.log(e)
}
return ajaxRequest;
}
//ENVIAR AL CONTROLADOR
function GuardarTodo(r1) {
//Variables de otros inputs
var nombre = $('#Nombre').val();
var descripcion = $('#Descripcion').val();
var tipo = $('#Tipo').val();
//Declaro el archivo
var documento = r1;
if (documento == "") {
//popup error
avisos.error("Video", "El video no ha podido ser guardado");
} else {
//GUARDAR VIDEO(No es la función real, esta compilado de cabeza,
//funciona bien la real)
var data = [{"Nombre": nombre, "Descripcion":descripcion, "Tipo":tipo, "Documento": documento}]
$.ajax({
type: "POST",
url: "URL",
contentType: false,
processData: false,
data: data
});
}
}
C#
[HttpPost]
public ActionResult MultimediaUpload()
{
try
{
var path = "";
if (Request.Files.Count > 0)
{
path = PdfUpload(Request.Files[0], "RUTA");
}
return Content(path);
}
catch (Exception e)
{
//LOG
_log.Error(e);
return null;
}
}
public string PdfUpload(HttpPostedFileBase file, string ruta)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
string path = Path.Combine(ruta, fileName);
file.SaveAs(path);
return path;
}
else
return null;
}
Now having all the code present, I know that being a heavy file I need to perform some task so that the execution of the script is paused until the file is loaded into data, I have tried with:
data.append("file", files[0]).then(ajaxRequest);
and also with setTimeout, but with this one, it doesn't matter how long you put it, it executes linearly and without pausing.
How can I do it so that regardless of the size of the video I assign it?
I don't know how IIS and .NET work , but considering that:
In PHP , two variables must be modified in the configuration file
php.ini
, one to increase the amount of data uploaded per formpost_max_size
and another for the maximum file sizeupload_max_filesize
.And searching a bit, I found the options that need to be modified in
web.config
:The first limits the size of the buffer received by POST, and the second limits the maximum size of a request at runtime that ultimately applies to sending the file.
These two settings should be set directly to number of bytes, in your case 106954752 / 1024 / 1024 = 102mb
Attention: You will probably have to increase it
executionTimeout
to prevent the upload of large files from expiring; Unfortunately, it does not have the option to set to zero (as in PHP ) to avoid limits, so you will have to test to determine the final value that you are going to assign, in seconds.