文件上传功能的疑问???
/* 文件上传预览图 */
function FileLoad(settings) {
this.input = settings.input;
this.imgView = settings.imgView;
this.delBtn = settings.delBtn;
this.init();
}
var image_base64="";
var image_name="";
FileLoad.prototype = {
init: function () {
var _this = this;
//
if (typeof(FileReader) === 'undefined') {
} else {
//“change”方法绑定,触发文件读取方法
this.input.on("change", function () {
_this.readFile.call(_this);
});
}
//del方法清除绑定信息
_this.delBtn.on("click", function (e) {
_this.imgView.html("");
_this.input.val("");
image_name="";
image_base64="";
$(this).hide();
})
},
readFile: function () {
var _this = this;
var file = this.input[0].files[0];//获取文件
if(file.size>3145728){
//alertWarning('请上传小于3M的图片','top');
alert('请上传小于3M的图片');
$("#fileInput").val("");
return false;
}
image_name=file.name;
var fileType = file.type;
if (fileType && !/image\/\w+/.test(fileType)) {
alert("请上传图片文件");
return false;
}
var $img = new Image();
//问题一:“webkitURL.createObjectURL”是什么作用?
var url = webkitURL.createObjectURL(file);
$img.onload = function() {
//生成比例
var width = $img.width;
var height = $img.height;
if (width>height){
if (width>600){
height = height * (600/width);
width = parseInt(600);
}
}else if (height > width){
if (height > 600){
width = width * (600/height);
height = parseInt(600);
}
}else {
if (width >600){
width = parseInt(600);
height = parseInt(600);
}
}
//生成canvas
var $canvas = document.createElement('canvas');
var ctx = $canvas.getContext('2d');
$canvas.width= width;
$canvas.height = height;
ctx.drawImage($img, 0, 0, width, height);
问题二:为什么要先将图片放入canvas中,然后再从canvas中获取图片?
image_base64 = $canvas.toDataURL('image/png',0.3);
image_base64=encodeURIComponent(image_base64);
问题三:“encodeURIComponent”作用是什么???
_this.imgView.html('<img src="'+url+'"/>');
_this.delBtn.show();
}
$img.src = url;
}
}
function FileLoad(settings) {
this.input = settings.input;
this.imgView = settings.imgView;
this.delBtn = settings.delBtn;
this.init();
}
var image_base64="";
var image_name="";
FileLoad.prototype = {
init: function () {
var _this = this;
//
if (typeof(FileReader) === 'undefined') {
} else {
//“change”方法绑定,触发文件读取方法
this.input.on("change", function () {
_this.readFile.call(_this);
});
}
//del方法清除绑定信息
_this.delBtn.on("click", function (e) {
_this.imgView.html("");
_this.input.val("");
image_name="";
image_base64="";
$(this).hide();
})
},
readFile: function () {
var _this = this;
var file = this.input[0].files[0];//获取文件
if(file.size>3145728){
//alertWarning('请上传小于3M的图片','top');
alert('请上传小于3M的图片');
$("#fileInput").val("");
return false;
}
image_name=file.name;
var fileType = file.type;
if (fileType && !/image\/\w+/.test(fileType)) {
alert("请上传图片文件");
return false;
}
var $img = new Image();
//问题一:“webkitURL.createObjectURL”是什么作用?
var url = webkitURL.createObjectURL(file);
$img.onload = function() {
//生成比例
var width = $img.width;
var height = $img.height;
if (width>height){
if (width>600){
height = height * (600/width);
width = parseInt(600);
}
}else if (height > width){
if (height > 600){
width = width * (600/height);
height = parseInt(600);
}
}else {
if (width >600){
width = parseInt(600);
height = parseInt(600);
}
}
//生成canvas
var $canvas = document.createElement('canvas');
var ctx = $canvas.getContext('2d');
$canvas.width= width;
$canvas.height = height;
ctx.drawImage($img, 0, 0, width, height);
问题二:为什么要先将图片放入canvas中,然后再从canvas中获取图片?
image_base64 = $canvas.toDataURL('image/png',0.3);
image_base64=encodeURIComponent(image_base64);
问题三:“encodeURIComponent”作用是什么???
_this.imgView.html('<img src="'+url+'"/>');
_this.delBtn.show();
}
$img.src = url;
}
}
...全文
相关推荐
JSP 实现文件上传功能
本文介绍如何使用JSP技术实现文件上传功能,包括两个页面:index.jsp用于用户交互和请求提交,index1.jsp负责处理文件上传逻辑,是整个功能的核心。
利用MultipartFile实现文件上传功能
通过使用MultipartFile,我们可以轻松实现文件上传功能,并且可以灵活地处理文件上传的逻辑。
JS 实现文件上传功能
在JavaScript的世界里,实现文件上传功能是一个常见的需求,尤其是在现代Web应用中。
vue实现文件上传读取及下载功能
本文将详细介绍如何使用Vue框架实现文件上传、读取和下载的基本功能。在Web开发过程中,文件的上传和下载是常见的功能需求,因此掌握这些技能对于Web开发者而言至关重要。
C#实现文件上传及文件下载功能实例代码
"C#实现文件上传及文件下载功能实例代码"在项目开发中,文件上传和下载是非常常见的需求。今天,我们将分享C#实现文件上传及文件下载功能实例代码,帮助开发者更好地解决文件上传和下载的问题。


