javascript - How does front-end js encapsulate a method or a jQuery plug-in to enable clicking a button to open the local file management system and upload files?
天蓬老师2017-05-16 13:36:56
0
3
743
To execute an event after clicking a button, open the local file management system, then select the file, and then select upload
A method encapsulated some time ago, using ajax and formData methods to implement file upload and display the upload progress during the upload process
js
$('#upload').on('click',function(){
var xhr = new XMLHttpRequest();
xhr.open('post','../PHP/post_file.php',true);
// 获取上传进度
xhr.upload.onprogress = (ev)=>{
var scale = Math.round( (ev.loaded/ev.total)*100 );
$('.text').html( scale + '%');
$('.progress').css('width', scale + '%');
};
xhr.onload = ()=>{
console.log('上传成功');
};
// 通过file表单的files属性拿到文件数据 通过formData将数据转换为二进制格式
var fileInfo = new FormData();
fileInfo.append( 'file',$('#iptFile')[0].files[0] );
// 发送数据
xhr.send(fileInfo);
});
A method encapsulated some time ago, using ajax and formData methods to implement file upload and display the upload progress during the upload process
js
html
css
php (php is not written by me)
I just encountered a problem with uploading images on the front end two days ago. This article is my record. See if it helps. Portal
You can use <input type="file" /> to achieve this, and then change the input style through css