2
我想从用户端上传我的项目的个人资料图片。此使用Javascript上传个人资料图片
我的HTML代码
<div class=" col-xs-12">
<div class="thumbnail">
<!--<img id="profilepic" class="profilepicholder" height="200" width="200">-->
<img id="profilepic" class="profilepicholder" src="#" alt="your image" height="200" width="200" />
<input type='file' />
<button id="save" class="save btn btn-primary btn-block">Save</button>
</div>
</div>
和JavaScript上传文件是
$(function() {
$(":file").change(function() {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
}
});
});
function imageIsLoaded(e) {
$('#profilepic').attr('src', e.target.result);
};
$(document).ready(function() {
profile_url = "";
upload_counter = 0;
uploaded_counter = 0;
/*
Function to carry out the actual PUT request to S3 using the signed request from the app.
*/
function upload_file(file, signed_request, url, type) {
var xhr = new XMLHttpRequest();
xhr.open("PUT", signed_request);
xhr.setRequestHeader('x-amz-acl', 'public-read');
xhr.onload = function() {
if (xhr.status === 200) {
console.log(url)
//this is the URL , keep the value in the variable
$(document).ready(function() {
/* $("#social_card").attr("value", url);
console.log("oi" + url);
*/ //national_id_url = url;
upload_counter = upload_counter + 1;
console.log(upload_counter);
if (type == "profilepic") {
profile_url = url;
}
});
}
};
xhr.onerror = function() {
alert("Could not upload file.");
};
xhr.send(file);
}
/*
Function to get the temporary signed request from the app.
If request successful, continue to upload the file using this signed
request.
*/
function get_signed_request(file, type) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "/sign_s3?file_name=" + $(".username").val() + "/" + file.name + "&file_type=" + file.type);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
upload_file(file, response.signed_request, response.url, type);
} else {
alert("Could not get signed URL.");
}
}
};
xhr.send();
}
/*
Function called when file input updated. If there is a file selected, then
start upload procedure by asking for a signed request from the app.
*/
function init_upload() {
console.log("here");
var profilepic_files = document.getElementById("profilepic").innerHTML;
var profilepic_file = profilepic_files[0];
if (profilepic_file == null) {
//alert("No file selected.");
} else {
get_signed_request(profilepic_file, "profilepic");
}
}
var myInterval;
$("#save").mouseup(function() {
$('.loadingcustom').css({
display: 'block',
position: 'absolute',
width: '200vw',
});
init_upload();
var profilepic_files = document.getElementById("profilepic").innerHTML;
var profilepic_file = profilepic_files[0];
if (profilepic_file != null) {
uploaded_counter = uploaded_counter + 1;
}
myInterval = setInterval(function() {
console.log(upload_counter);
console.log(uploaded_counter);
if (upload_counter == uploaded_counter) {
upload_counter = uploaded_counter + 1;
var userData = {
"profile_url": profile_url,
};
console.log(userData);
$.ajax({
type: "POST",
url: "https://stackoverflow.com/users/",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(userData),
success: function (data) {
console.log("user created");
$('.alert').show();
$('.loadingcustom').hide();
$('.savebtn').show();
clearInterval(myInterval);
setTimeout(function() {
window.location.reload();
}, 2000);
}
});
}
}, 3000);
});
});
我上传这个文件的MongoDB。 我的要求是为特定用户上传图片。
为什么你不使用一些插件? http://fineuploader.com/例如 – Legotin
感谢您的建议。其实,我不想为这个项目使用任何插件。 @legotin –