JS 讀取本地文件轉base64 上傳到服務器


$("#qrcode").on("change", function(){
  var bs64 = base64(document.getElementById("qrcode"));
});
// 商品評價 圖片轉base64
// Process the file
// Precondition: expects image files, but works with others too
function base64(file) {
    var reader = new FileReader();
    var pos = file.target.files[0].name.lastIndexOf(".");
    var type = file.target.files[0].name.substring(pos + 1);

    if (type.toLowerCase() != "png" && type.toLowerCase() != 'jpg' && type.toLowerCase() != 'jpeg' && type.toLowerCase() != 'gif' && type.toLowerCase() != 'bmp') {
        alert("格式錯誤,請上傳'png、jpg、jpeg、bmp、gif'格式文件");
        return;
    }
    num += 1;
    reader.onloadend = (
        function(e) {
            imgurl = e.target.result;
            updateBackground();
        }
    );

    // Read the file
    reader.readAsDataURL(file.target.files[0]);
}


// Return the data to the user
function updateBackground(){
  // urlElement.innerHTML = imgurl;
   
  jq.ajax({
    url:'user.php',
    data:{
      act:'update_pic',
      imgurl:imgurl
    },
    type:'post',
    dataType:'text',
    success:function(data){
      if(data == "error"){
        alert("上傳失敗,請重新操作");
      }else{
        jq("#comment_image").prepend("<input type='hidden' name='images[]' value='"+data+"'>");
        jq("#comment_image").prepend("<img width='70' height='70' src='"+imgurl+"' style='margin: 2px;'>");
        num-=1;
       sum+=1;
      }   
    }
  })

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM