图片执行file操作

<input type="file" id="file1" name="file1" accept=".jpg,.png" style="display:none" onchange="showPicture(this);">
<img src="../images/y_add_photo.png" onclick="uploadFile('file1')">

function uploadFile(obj) {
$("#" + obj).click();
}
var fileArr = [];
function showPicture(obj) {
var fileList = obj.files;
var length = fileList.length;
for (var i = 0; i < length; i++) {
var suffix = fileList[i].name.substring(fileList[i].name.lastIndexOf(".") + 1, fileList[i].name.length);
if (suffix == "jpg" || suffix == "png") {
fileArr.push(fileList[i]);
var objUrl = getObjectURL(fileList[i]);
if (objUrl) {
var html = '<div class="y_photo_close"><img style="width:66px;height:66px;" src="' + objUrl + '"><img class="y_close_img" title="删除" onclick="deletePicture(this)" src="../images/y_shan.png"></div>';
$('.y_photo_int').prepend(html);
}
}
else {
layer.msg("请选择jpg或png格式的图片!");
return;
}
}
}

function deletePicture(obj) {
var index = $(".y_photo_int div").index($(obj).parent());
fileArr.splice(index, 1);
$(obj).parent().remove();
$('#file1').val('');
$("#image_path").val('');
}
function getObjectURL(file) {
var url = null;
if (window.createObjectURL != undefined) {
url = window.createObjectURL(file);
} else if (window.URL != undefined) {
url = window.URL.createObjectURL(file);
} else if (window.webkitURL != undefined) {
url = window.webkitURL.createObjectURL(file);
}
return url;
}

猜你喜欢

转载自www.cnblogs.com/yyjspace/p/11606415.html