Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda