


JS code implementation for browser image selection, preview, rotation, and batch upload_javascript skills
I studied the business scenarios I encountered at work with my colleagues, mainly for compatibility with the IE version
In fact, I collected some trivial knowledge points and solutions on the Internet, and then integrated them. The main points are as follows:
1. IE input type=file image preview requires IE’s filter css
progid:DXImageTransform.Microsoft.AlphaImageLoader
Chrome/firefox uses the file reader of File api
2. For image rotation, IE uses the filter of progid:DXImageTransform.Microsoft.Matrix (filters can be combined and separated by spaces)
Use canvas for chrome/firefox
3. To upload images, I use the form in the invisible iframe to dynamically add input[type=file] to achieve it. Chrome/firefox can use xhr, but I am too lazy to modify it
4. In order to upload images without refreshing this page, and to be able to select files repeatedly, an iframe is used to maintain a list of input[type=file], which is quite clever.
You can refer to the code below, which is mainly a main html, and then two iframe html. The data returned by the uploaded server is the file name of the successfully uploaded file, which is used to delete the preview image.
// 上传回调
// resultList -> ['file1', 'file2'] 为上传成功的file name
var uploadCallback = function(resultList){
console.log(JSON.stringify(resultList));
var i = 0;
for(; i < resultList.length; i++){
var index = resultList[i].substr('file'.length);
$(':checkbox[value=' + index + ']').parent().parent().remove();
}
};
$(function(){
// 保存图片旋转的角度,以便提交给服务端处理
var rotateAng = {};
// 用于命名后缀的序号
var cc = 0;
// 如果是chrome/ff,需要用file api去生成img
var genImgTpl = function(input, index){
return '';
};
var readImgFromInput = function(_input, index){
var inputDom = _input[0];
// chrome/ff
if(inputDom['files']){
var reader = new FileReader();
reader.onload = function(e){
$('img.main:last').attr({src : e.target.result});
}
reader.readAsDataURL(inputDom['files'][0]);
}else{
var src = _input[0].value;
var imgDom = $('#img' + index);
imgDom.attr('src-old', src);
imgDom.css({
float: 'left',
position: 'relative',
overflow: 'hidden',
width: '195px',
height: '195px'
});
imgDom.css({'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='scale',src=\"" + src + "\")"});
}
};
var showImg = function(_input){
var index = ++cc;
_input.addClass('hide');
_input.attr('name', 'file' + index);
_input.attr('data-index', index);
var iframeWin = $('#choose')[0].contentWindow;
iframeWin.addInput(_input);
var tpl = '
'' +
'

'
$('#imgDiv').append(tpl);
readImgFromInput(_input, index);
};
var addAnother = function(){
$('#uploadBtn').before('');
};
// input[type=file]的绑定事件
$('#uploadDiv input').live('change', function(){
var path = this.value;
if(!path){
return;
}
showImg($(this));
addAnother();
});
// 可以在checkbox时候remove input
// $('#imgDiv input:checkbox').live('change', function(){
// var isChecked = $(this).is(':checked');
// console.log(isChecked);
// });
$('#imgDiv span.turn-right').live('click', function(){
// 上次旋转的角度
var index = $(this).siblings('span.choose').find('input').val();
var oldAng = rotateAng[index] || 0;
var newAng = oldAng + 90;
rotateAng[index] = newAng;
$('#img' + index).rotate(90);
});
// 表单提交时候根据checkbox,删除未choose的input[type=file]
$('#uploadBtn').click(function(){
var choosedNum = $('#imgDiv input:checkbox').filter(':checked').length;
if(!choosedNum){
alert('请选择上传文件!');
return false;
}
// 选中的序号数组
var choosedIndexList = $('#imgDiv input:checkbox').filter(':checked').map(function(){
return this.value;
}).get();
.
var chooseIframe = $('#choose')[0].contentWindow;
var i = 0;
var index = chosenIndexList[i];
var inputFile = chooseIframe.$('#uploadDiv input' ).filter('[data-index=' index ']');
uploadIframe.$('#uploadForm').append(inputFile);
var tplInput = ''; }
uploadIframe.doUpload();
return false;
});

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to implement image preview function in uni-app Introduction: In mobile application development, image preview is a commonly used function. In uni-app, we can implement the image preview function by using uni-ui plug-ins or custom components. This article will introduce how to implement the image preview function in uni-app, with code examples. 1. Use the uni-ui plug-in to implement the image preview function. uni-ui is a component library based on Vue.js developed by DCloud, which provides a rich UI group.

How to handle image preview and zoom issues in Vue components requires specific code examples. Introduction: In modern web applications, image preview and zoom are very common requirements. As a popular front-end framework, Vue provides us with many powerful tools to deal with these problems. This article will introduce how to handle image preview and zoom in Vue components, and provide specific code examples. 1. Image preview: Image preview means that when the user clicks or hovers over the image, it can display a large version of the image or enlarge it in a specific area.

How to use JavaScript to achieve image rotation effect? In web development, we often encounter scenarios where image rotation effects need to be achieved, such as displaying 360° rotation images of products, achieving image carousel effects, etc. JavaScript is a powerful scripting language that can easily achieve this image rotation effect. The following will introduce a method to achieve image rotation effects based on JavaScript and provide specific code examples. First, we create a simple HTML structure

Using uniapp to implement image rotation function In mobile application development, we often encounter scenarios where images need to be rotated. For example, the angle needs to be adjusted after taking a photo, or an effect similar to the rotation of a camera after taking a photo is achieved. This article will introduce how to use the uniapp framework to implement the image rotation function and provide specific code examples. uniapp is a cross-platform development framework based on Vue.js, which can simultaneously develop and publish applications for iOS, Android, H5 and other platforms. Implemented in uniapp

To implement the picture rotation effect in WeChat Mini Program, specific code examples are required. WeChat Mini Program is a lightweight application that provides users with rich functions and a good user experience. In mini programs, developers can use various components and APIs to achieve various effects. Among them, the picture rotation effect is a common animation effect that can add interest and visual effects to the mini program. To achieve image rotation effects in WeChat mini programs, you need to use the animation API provided by the mini program. The following is a specific code example that shows how to

How to implement image rotation using PHP and GD libraries Image rotation is a common image processing requirement. By rotating images, you can achieve some special effects or meet user needs. In PHP, you can use the GD library to implement the image rotation function. This article will introduce how to use PHP and the GD library to implement image rotation, with code examples. First, make sure your PHP environment has the GD library extension installed. Enter php-m on the command line to check if there is a gd module. If not, you need to install it first. Here is a simple

How to implement image browsing and image preview functions in uniapp? In uniapp, we can use the uni-ui component library to implement image browsing and image preview functions. uni-ui is a component library based on Vue.js developed by DCloud, which provides a rich set of UI components, including image browsing and image preview components. First, we need to introduce the uni-ui component library into the project. Open the pages.json file of the project and add "un" in the "easycom" field

Vue is a popular JavaScript framework for building single page applications (SPA). Image preview is a common feature in web applications, and there are many ways to implement image preview in Vue. This article will introduce in detail the techniques and best practices for implementing the image preview function in Vue. 1. Use the Vue plug-in The Vue plug-in provides a simple way to implement image preview. Vue plugins can be registered globally so they can be used throughout the application. Here are two commonly used Vue plug-ins:
