Upload image using javascript

前端 未结 3 1051
夕颜
夕颜 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.

    <div>
         <div><img id="logo" class="img-polaroid" alt="Logo" src="' . $row['logo'] . '" title="Click to change the logo" width="128">
         <input style="visibility:hidden;" id="logoupload" type="file" accept="image/* ">
    </div>
    
    
    
      $('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(){
    
                                });
                                }
    
                            });
    
    0 讨论(0)
  • 2020-12-16 02:26

    The reason that im.fileSize is only working in IE is because ".fileSize" is only an IE property. Since you have code that works in IE, I would do a check for the browser and render your current code for IE and try something like this for other browsers.

    var imgFile = document.getElementById('submitfile');
    if (imgFile.files && imgFile.files[0]) {
        var width;
        var height;
        var fileSize;
        var reader = new FileReader();
        reader.onload = function(event) {
            var dataUri = event.target.result,
            img = document.createElement("img");
            img.src = dataUri;
            width = img.width;
            height = img.height;
            fileSize = imgFile.files[0].size;
            alert(width);
            alert(height);
            alert(fileSize);
       };
       reader.onerror = function(event) {
           console.error("File could not be read! Code " + event.target.error.code);
       };
       reader.readAsDataURL(imgFile.files[0]);
    }
    

    I haven't tested this code but it should work as long as I don't have some typo. For a better understanding of what I am doing here check out this link.

    0 讨论(0)
  • 2020-12-16 02:38
    $('#imagess').change(function(){
    
            var total_images=$('#total_images').val();
    
    
            var candidateimage=document.getElementById('imagess').value;
            formdata = false;
            var demo=document.getElementById("imagess").files;
    
            if (window.FormData) {
               formdata = new FormData();
               }
    
              var i = 0, len = demo.length, img, reader, file;
             for ( ; i < len; i++ ) {
              file = demo[i];
              if (file.type.match(/image.*/)) {
               if (formdata) {
                formdata.append("images", file);
    
               }
              } 
             }
    
    
            $('#preview').html('Uploading...');
            var url=SITEURL+"users/image_upload/"+total_images;
    
            $.ajax({
                url: url,
                type: "POST",
                data: formdata,
                processData: false,
                contentType: false,
                success: function (res) {
                    $('#preview').html('');
                    if (res == "maxlimit") {
                        alert("You can't upload more than 4 images");
                    }
                    else if (res == "error") {
                        alert("Image can't upload please try again.")
                    }
                    else {
                        $('#user_images').append(res);
    
    
                    }
                }
               });  
    
    
        });
    
    0 讨论(0)
提交回复
热议问题