1. 程式人生 > >上傳頭像

上傳頭像

targe files this 直接 mda position ons png try

<div class="commodityImg_evaluate">
<div class="ge_pic_icon_Infor">
<img onclick="getElementById(‘file‘).click()" title="點擊添加圖片" alt="點擊添加圖片" src="images/tianjia.png">
</div>
<div class="Infor_file">
<input type="file" multiple="multiple" id="file" name=‘img‘ style="height:0;width:0;z-index: -1; position: absolute;" onchange="getPhoto(this)"/>
</div>
</div>

// 調用相冊
function getPhoto(node) {
var imgURL = "";
try{
var file = null;
if(node.files && node.files[0] ){
file = node.files[0];
}else if(node.files && node.files.item(0)) {
file = node.files.item(0);
}
//Firefox 因安全性問題已無法直接通過input[file].value 獲取完整的文件路徑
try{
imgURL = file.getAsDataURL();
}catch(e){
imgRUL = window.URL.createObjectURL(file);
}
}catch(e){
if (node.files && node.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
imgURL = e.target.result;
};
reader.readAsDataURL(node.files[0]);
}
}
creatImg(imgRUL);
return imgURL;
}
function creatImg(imgRUL){
var textHtml = "<img src=‘"+imgRUL+"‘/>";
$(".myNews_Headportrait").html(textHtml);
//uoloadImg();
//console.log(imgRUL)
}
// 上傳圖片
$("#file").change(function(){
//創建FormData對象
var dataImg = new FormData();
//為FormData對象添加數據
$.each($(‘#file‘)[0].files, function(i, file) {
dataImg.append(‘img‘, file);
});
$.ajax({
url:‘‘+url+‘/appupload/upload‘,
type:‘POST‘,
data:dataImg,
cache: false,
contentType: false, //不可缺
processData: false, //不可缺
success:function(data){
console.log(data);
//data = $(data).html();
}
});
});

上傳頭像