网站首页 > 技术文章 正文
<style>
.progress {
width:9%;
height:10px;
border:1px solid #ccc;
margin-bottom:6px;
margin-left: 85px;
margin-top: -10px;
display: none;
}
.bgpro {
display:block;
width:9px;
background:#c0c4cf;
height:10px;
color:#fff;
text-align: center;
font-size: 8px;
}
</style>
<div id="preview">
<img id="imghead" class="pic_show" border=0 src="__STATICS_DOMAIN__/public/boss/images/imgbg.png" width="100" height="100" />
</div>
<input type="button" value="选择logo" class="btn" />
<input type="file" onchange="previewImage(this)" class="file" name="pic"/>
<a>(请选取 100 X 100正方形logo以便达到最佳展示效果)</a>
</div>
<div class="progress"><span class="bgpro"></span></div>
$('input[name="pic"]').on('change',function() {
if(typeof this.files == 'undefined'){
return;
}
var img = this.files[0];//获取图片信息
var type = img.type;//获取图片类型,判断使用
var url = getObjectURL(this.files[0]);//使用自定义函数,获取图片本地url
var fd = new FormData();//实例化表单,提交数据使用
fd.append('pic',img);//将img追加进去
if(url)
$('.pic_show').attr('src',url).show();//展示图片
if(type.substr(0,5) != 'image'){//无效的类型过滤
alert('非图片类型,无法上传!');
return;
}
//开始ajax请求,后台用的tp
$.ajax({
type : 'post',
url : dolphin.image_upload_url,
data : fd,
processData: false, // tell jQuery not to process the data ,这个是必须的,否则会报错
contentType: false, // tell jQuery not to set contentType
dataType : 'text',
xhr : function() {//这个是重点,获取到原始的xhr对象,进而绑定upload.onprogress
var xhr = jQuery.ajaxSettings.xhr();
xhr.upload.onprogress = function(ev) {
//这边开始计算百分比
var parcent = 0;
if(ev.lengthComputable) {
percent = 100 * ev.loaded / ev.total;
percent = parseFloat(percent).toFixed(2);
$('.progress').show();
$('.bgpro').css('width',percent + '%').html(percent + '%');
}
};
return xhr;
},
success:function(data){
alert(data)
}
});
});
//自定义获取图片路径函数
function getObjectURL(file) {
var url = null ;
if (window.createObjectURL!=undefined) { // basic
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
return url ;
}
PHP
- Array
- (
- [name] => cooff
- [email] => qq.comaa
- )
- Array
- (
- [file] => Array
- (
- [name] => yunce2.0测试.apk
- [type] => application/vnd.android.package-archive
- [tmp_name] => /tmp/php4Jxt0c
- [error] => 0
- [size] => 6476627
- )
- )
图片上传插件
function previewImage(file) {
console.log(4444)
var MAXWIDTH = 180;
var MAXHEIGHT = 180;
var div = document.getElementById('preview');
if(file.files && file.files[0]) {
div.innerHTML = '<img id=imghead>';
div.innerHTML +='<span class="closeimg" onclick="closeimg()"></span>';
var img = document.getElementById('imghead');
img.onload = function() {
console.log(5555)
var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
img.width = rect.width;
img.height = rect.height;
// img.style.marginLeft = rect.left+'px';
img.style.marginTop = rect.top + 'px';
}
var reader = new FileReader();
reader.onload = function(evt) {
img.src = evt.target.result;
}
reader.readAsDataURL(file.files[0]);
} else //兼容IE
{
var sFilter = 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
file.select();
var src = document.selection.createRange().text;
div.innerHTML = '<img id=imghead>';
var img = document.getElementById('imghead');
img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
status = ('rect:' + rect.top + ',' + rect.left + ',' + rect.width + ',' + rect.height);
div.innerHTML = "<div id=divhead style='width:" + rect.width + "px;height:" + rect.height + "px;margin-top:" + rect.top + "px;" + sFilter + src + "\"'></div>";
}
}
function clacImgZoomParam(maxWidth, maxHeight, width, height) {
var param = {
top: 0,
left: 0,
width: width,
height: height
};
if(width > maxWidth || height > maxHeight) {
rateWidth = width / maxWidth;
rateHeight = height / maxHeight;
if(rateWidth > rateHeight) {
param.width = maxWidth;
param.height = Math.round(height / rateWidth);
} else {
param.width = Math.round(width / rateHeight);
param.height = maxHeight;
}
}
param.left = Math.round((maxWidth - param.width) / 2);
param.top = Math.round((maxHeight - param.height) / 2);
return param;
}
function closeimg(){
$('#preview').find('img').attr('src','../public/boss/images/imgbg.png');
$('#preview').find('span').remove();
console.log($('#preview').find('input').val());
var file = document.getElementById("fileInput");
if (file.outerHTML) {
file.outerHTML = file.outerHTML;
} else { // FF(包括3.5)
file.value = "";
}
}
猜你喜欢
- 2024-09-12 不得不佩服,美观小巧的网页内容编辑器——ContentTools
- 2024-09-12 监听设备方向变化?分享 1 段优质 JS 代码片段!
- 2024-09-12 一分钟了解ajax。(一分钟了解网络广告)
- 2024-09-12 Jquery一个简单的注册验证(jquery注册点击事件)
- 2024-09-12 [前端请求]Ajax知识点 Jquery接口封装 fetch原生js请求
- 2024-09-12 JQuery笔记(下)(jquery gt)
- 2024-09-12 Vue.js 快速上手(vue.js怎么学)
- 2024-09-12 Ajax 的全面总结(ajax概述)
- 2024-09-12 html file选择文件一次之后就失效了怎么办
- 2024-09-12 jQuery入门2(jquery入门教程)
- 最近发表
- 标签列表
-
- cmd/c (64)
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- sqlset (64)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)