Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
Picture upload interrupted in JavaScript

Consider the following script:

    function uploadPicture(myindex, stringid)
{
    uploadingpicture = true;
    let fileform = document.getElementById("fileform" + myindex);
    let file = fileform.elements.namedItem('file').files[0];
    let type = file.type;
    let size = file.size;
    console.log('User is trying to upload a ' + type + ' file called ' + stringid + ' (size: ' + size + ')');

    let contents = new FileReader(); // no arguments
    contents.readAsDataURL(file);

    contents.onloadend = function ()
    {

        let filename;
        let client;
        let filecontainer = document.getElementById('file' + myindex);
        if (filecontainer != null)
        {
            filename = filecontainer.innerHTML.replace(/\+/g, '+').replace(/&/g, '&').replace(/#/g, '#').replace(/%/g, '%');
        }
        let clientcontainer = document.getElementById('myclient');
        if (clientcontainer != null)
        {
            client = clientcontainer.innerHTML.replace(/\+/g, '+').replace(/&/g, '&').replace(/#/g, '#').replace(/%/g, '%');
        }

        postRequest(
            'ajaxcalls/ajaxUploadPicture.php', // url
            '&client=' + client +
            '&stringid=' + stringid +
            '&filename=' + filename +
            '&field=' + myindex +
            '&type=' + type +
            '&contents=' + contents.result,
            function (responseText)
            {
                return drawOutputUploadPicture(myindex, responseText);
            },  // success
            drawErrorUploadPicture   // error
        );
    }

    contents.onerror = function ()
    {
        console.log(contents.error);
    };

}

function drawOutputUploadPicture(myindex, responseText)
{
    console.log('RESPONSE: ' + responseText);
    let feedbackElement = document.getElementById('feedback' + myindex);
    if (feedbackElement !== null)
    {
        feedbackElement.innerHTML = responseText;
    }
    uploadingpicture = false;
}

function drawErrorUploadPicture()
{
    uploadingpicture = false;
}

This script works. However, the upload is almost always interrupted halfway, so the result is a partial picture: sometimes you see half, sometimes a quarter, but almost never the whole picture. There are no errors. There is no upload limit.

What could be causing this?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I found out the cause of this.

The form calling this JavaScript used a Submit button with type="submit", so that the form being submitted interrupted the actual upload. I was able to solve this by changing the type into type="button".

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda