Sending image with Jquery

Asked

Viewed 82 times

0

I searched on SOF how to send image with Jquery, tested several and did not work, and others only for form field, the event should send the image with input OUTSIDE/WITHOUT form.

Input (No form):

<input id='ads-files-edit' type='file' accept='image/*' placeholder='Selecione a imagem' data-max-size='5242880' class='form-control form-group-lg ads-files-edit'>

Jquery:

<script type="text/javascript">
    $(function () {
        $("#ads-files-edit").change(function () {

        });
    }
</script>
  • The absence of an element <form> is a semantic error (only a warning). I’m not sure, but I think this is not possible, a while ago I tried to use jQuery to download a PDF but it didn’t work because the Blob type is not supported by the AJAX part of the library

  • https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

  • https://i.stack.Imgur.com/evLUR.png

  • Thank you all for your cooperation.

1 answer

0


No form and no Submit button

index.html

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id='ads-files-edit' type='file' accept='image/*' placeholder='Selecione a imagem' data-max-size='5242880' class='form-control form-group-lg ads-files-edit'>

<script language="javascript">

$(document).ready(function() {
    $('#ads-files-edit').change(function(){
        var file_data = $('#ads-files-edit').prop('files')[0];   
        var form_data = new FormData();                  
        form_data.append('file', file_data);
        $.ajax({
            url: "upload-imagem.php",
            type: "POST",
            data: form_data,
            contentType: false,
            cache: false,
            processData:false,
            success: function(data){
                 $("#result").html(data);
            }
        });
    });
})

</script>

<div id="result"></div>

upload-image.php

<?php 

$userFile=$_FILES['file'];
$name = $userFile['name'];
$tmp = $userFile ['tmp_name'];

if(empty($userFile)):
   echo '<a href="uploadFoto.htm">Selecione um arquivo para fazer upload</a>';
else:
    if(move_uploaded_file($tmp, 'uploads/'.$name)):
            echo 'Arquivo enviado com sucesso';
    else:
            echo 'Error';
    endif;
endif;

?>
  • Thanks, but I have no way to score, I want to ask a question, if I have more than one input file and instead of using the ID, use the class and $(this) for what was changed, can help?

  • @Angeloj, How so dar pontuação?

  • @Angeloj, I see no need for multiple inputs if with an input you can do as many uploads as you want. Click once send an image, click again send another image and so it goes ....

  • Grateful for the help, it worked, I say click the arrow up as a functional response.

Browser other questions tagged

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