Sending Image of the Formdata file without input file

Asked

Viewed 309 times

0

I am working with Cordova, and it does not support the html input file. I wonder if there is some way to send an image of the file by the ajax formData method.

I tried to send the image as Blob to my database, but I can’t bring the original image back.

When I tested with inpt file, it sent the image without problem with a Contenttype parameter: "image/jpeg", I wondered if there was any way to create an equal parameter to send my image with a formData.append('image', image);

There’s also the file transfer, but I can’t send the image, and I wanted something simpler and more objective to send the image.

Thank you

$(document).ready(function(){
// var formulario = document.getElementById('iformulario'); 
// var formData = new FormData(formulario);

// var blob = new Blob([imagem]);

var formData = new FormData();
formData.append('nnome', nome);
formData.append('nemail', email);
formData.append('nestado', estado);
formData.append('nsenha1', senha1);
formData.append('ntipo',tipo);
// formData.append('nimagem', blob, localStorage.getItem('imagemcadastro'));
// formData.append('nimagem', imagem);

$.ajax({
    type:'post',                                        //Definimos o método HTTP usado
    dataType: 'json',                                   //Definimos o tipo de retorno
    url: 'cadastro.php',    //Definindo o arquivo onde serão buscados os dados
    contentType: "multipart/form-data",
    // headers:{'Content-Type':'image/jpeg','X-Requested-With':'XMLHttpRequest'},
    // ContentType: "image/jpeg",
    data: formData,

    contentType: false, 
    processData: false,

    success: function(dados){
        if (dados == 1){
            alert("Usuario Existente");
        }

        if (dados == 0){
            alert("Não foi possível cadastrar seu email");
        }

        if (dados == 2) {
            alert("Cadastro Realizado com Sucesso");
            window.location = "../index.html";      

        }
    }
    });

});

function editandoavatar() {

// var tranfere = localStorage.getItem('imagemcadastro');
var tranfere = document.getElementById('carregafoto').src;

var id = localStorage.getItem('id');
    // var tranfere = "IMG_0004.jpg";
    var server = "editaavatar.php";

    var options = new FileUploadOptions();
    options.fileKey = "nimagem";
    options.fileName = tranfere.substr(tranfere.lastIndexOf('/') + 1);
    options.mimeType = "image/jpg";
    options.httpMethod = 'POST';

    options.params ={ 
        nid: id 
    };

    console.log(tranfere);
    console.log(options);
    console.log(id);

    var ft = new FileTransfer();
    ft.upload(tranfere, server, win, fail, options);

    function win(r) {
                console.log("Should not be called.");
            }

            function fail(error) {
                // error.code == FileTransferError.ABORT_ERR
                alert("An error has occurred: Code = " + error.code);
                console.log("upload error source " + error.source);
                console.log("upload error target " + error.target);
            }
  • If possible post the code you tried.

  • added the codes

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.