Upload image using javascript

前端 未结 3 1054
夕颜
夕颜 2020-12-16 01:51

I\'m trying to get image as Object of javascript on the client side to send it using jQuery





        
3条回答
  •  攒了一身酷
    2020-12-16 02:16

    This is what I use and it works great for me. I save the image as a blob in mysql. When clicked, the file upload dialog appears, that is why i set the file input visibility to hidden and set its type to upload image files. Once the image is selected, it replaces the existing one, then I use the jquery post method to update the image in the database.

    $('img#logo').click(function(){ $('#logoupload').trigger('click'); $('#logoupload').change(function(e){ var reader = new FileReader(), files = e.dataTransfer ? e.dataTransfer.files : e.target.files, i = 0; reader.onload = onFileLoad; while (files[i]) reader.readAsDataURL(files[i++]); }); function onFileLoad(e) { var data = e.target.result; $('img#logo').attr("src",data); //Upload the image to the database //Save data on keydown $.post('test.php',{data:$('img#logo').attr("src")},function(){ }); } });

提交回复
热议问题