Ajax formData上传文件 + 进度条
发布时间:2020-12-16 03:13:03 所属栏目:百科 来源:网络整理
导读://require('./lib/ajax');require('jquery');var type=0,//0 dp 1 mt localHost = location.host.split('.'),localUrl = location.origin.search(/xxxx/g) -1 ? '//g.xxxx.com' -1 : '//g.xxxx.com'; if(location.origin.match('meituan')) { type=1; }$(".f
// require('./lib/ajax'); require('jquery'); var type=0,//0 dp 1 mt localHost = location.host.split('.'),localUrl = location.origin.search(/xxxx/g) > -1 ? '//g.xxxx.com' > -1 : '//g.xxxx.com'; if(location.origin.match('meituan')) { type=1; } $(".file").on('change',function(es){ $(".pops").removeClass('hide'); console.log('kaishi'); var file = $(".file")[0].files[0]; //文件对象 uploadVideo(file) }) function uploadVideo(file){ console.log('show'); var name = file.name,//文件名 form = new FormData(); form.append("file",file); if(file.size>10000000){ alert('请上传低于10兆的视频文件') window.location.href='upload.html?t=123' return } var xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress",uploadProgress,false); xhr.addEventListener("load",uploadComplete,false); xhr.addEventListener("error",uploadFailed,false); xhr.addEventListener("abort",uploadCanceled,false); xhr.open("POST",'//g.xxxx.com/xxxx/xxxx/demo/uploadImage');//修改成自己的接口 xhr.send(form); } function uploadProgress(){ console.log('progress'); $(".pops").removeClass('hide'); $(".pops").show(); } function uploadComplete(evt){ var data = evt.target.responseText; if(typeof data==="string") { data = JSON.parse(data); } var id = data.data; $(".dp").show() $(".mt").show() $(".pops").addClass('hide'); } function uploadFailed(){ console.log('uploadFailed'); } function uploadCanceled(){ console.log('uploadCanceled'); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |