2015-09-02 51 views
0

所以我得到这个脚本,应该输出base64编码上传文件的文件内容,问题是,它也输出像data:image/jpeg; base64,在编码的开头输出。我需要做什么,这个脚本只输出上传文件的编码文件内容而不是数据:image/jpeg; base64等?Javascript文件内容

<html> 
<head> 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css"> 
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script> 
    <script src="http://code.jquery.com/ui/1.11.3/jquery-ui.js"></script> 
</head> 
<body> 
    <script> 
     function PreviewImage() { 
     var oFReader = new FileReader(); 
     oFReader.readAsDataURL(document.getElementById("uploadImage").files[0]); 
     oFReader.onload = function (oFREvent) { 
      var sizef = document.getElementById('uploadImage').files[0].size; 
      document.getElementById("uploadPreview").src = oFREvent.target.result; 
      document.getElementById("uploadImageValue").value = oFREvent.target.result; 
     }; 
    }; 
    jQuery(document).ready(function(){ 
     $('#viewSource').click(function() 
     { 
      var imgUrl = $('#uploadImageValue').val(); 
      document.write(imgUrl); 

     }); 
    }); 
    </script> 
    <div> 
     <input type="hidden" id="uploadImageValue" name="uploadImageValue" value="" /> 
     <img id="uploadPreview" style="width: 150px; height: 150px;" /><br /> 
     <input id="uploadImage" style="width:120px" type="file" size="10" name="myPhoto" onchange="PreviewImage();" /> 
    </div> 
    <a href="#" id="viewSource">Source file</a> 
</body> 

回答

0

变化的代码来

document.write(imgUrl.split(',').pop()); 
        ^^^^^^^^^^^^^^^^^ 

var imgUrl = $('#uploadImageValue').val().split(',').pop();      
             ^^^^^^^^^^^^^^^^^