Skip to content

Instantly share code, notes, and snippets.

@3014zhangshuo
Created February 14, 2017 06:51
Show Gist options
  • Save 3014zhangshuo/cb0cf05889ff25411e96a592fc2acfb9 to your computer and use it in GitHub Desktop.
Save 3014zhangshuo/cb0cf05889ff25411e96a592fc2acfb9 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
//图片1
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#img-1').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#resume_past_project_image1").change(function(){
readURL(this);
});
//图片2
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#img-2').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#resume_past_project_image2").change(function(){
readURL(this);
});
//图片3
function readURL(input) {
var uploadImage = $('#resume_past_project_image3');
uploadImage.replaceWith(uploadImage = uploadImage.clone(true));
$("#resume_past_project_image3").on("change", function(){
// Get a reference to the fileList
var files = !!this.files ? this.files : [];
// If no files were selected, or no FileReader support, return
if (!files.length || !window.FileReader) return;
// Only proceed if the selected file is an image
if (/^image/.test( files[0].type)){
// Create a new instance of the FileReader
var reader = new FileReader();
// Read the local file as a DataURL
reader.readAsDataURL(files[0]);
// When loaded, set image data as background of div
reader.onloadend = function(){
$("#img-3").css("background-image","url("+this.result+")");
}
}}});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment