中山手机网站建设费用,广告公司怎么做业务,网站页面设计效果图,装修设计合同范本摘要
很多场景下需要选择多张图片上传#xff0c;或者是批量上传以提高效率#xff0c;多图上传的需求自然就比较多了#xff0c;本文使用最简单的XMLHttpRequest异步上传图片。
界面 上传示例 代码
index.html
!DOCTYPE html
htmlheadtitl…摘要
很多场景下需要选择多张图片上传或者是批量上传以提高效率多图上传的需求自然就比较多了本文使用最简单的XMLHttpRequest异步上传图片。
界面 上传示例 代码
index.html
!DOCTYPE html
htmlheadtitle多图上传/titlemeta charsetutf-8style#fileInput{width: 500px;height: 45px;margin: 50px auto 0;background: #eee;display: block;padding: 20px 20px;border-radius: 20px;}#previewContainer{width: 500px;margin: 10px auto;background: #eee;padding: 20px 20px;display: none;}.preview-image {max-width: 200px;max-height: 200px;margin-bottom: 10px;}/style/headbody!--选择文件--input typefile idfileInput acceptimage/* multiplediv idpreviewContainer/divscriptconst fileInput document.getElementById(fileInput);const previewContainer document.getElementById(previewContainer);// 监听选择文件fileInput.addEventListener(change, handleFileSelect);function handleFileSelect(event) {const files event.target.files;for (let i 0; i files.length; i) {const file files[i];const reader new FileReader();reader.onload function(event) {const image document.createElement(img);image.className preview-image;image.src event.target.result;previewContainer.appendChild(image);// 将文件上传至服务器uploadImage(file);}reader.readAsDataURL(file);}}// 将文件上传至服务器function uploadImage(file) {const xhr new XMLHttpRequest();const formData new FormData();// 将文件添加到formData对象formData.append(image, file);// 设置XHR请求的处理函数xhr.onreadystatechange function() {if (xhr.readyState XMLHttpRequest.DONE) {if (xhr.status 200) {console.log(上传成功);// 显示图片预览区域document.querySelector(#previewContainer).setAttribute(style, display:block);// 打印JSONconsole.log(JSON.parse(xhr.response))} else {console.log(上传失败);}}}// 发送POST请求到服务器xhr.open(POST, upload.php, true);xhr.send(formData);}/script/body
/htmlupload.php 请建立一个upload文件夹以存放上传的文件
?php// 编码header(Content-type:application/json);// 检查是否有文件上传if (isset($_FILES[image])) {// 获取上传的文件信息$file $_FILES[image];// 获取文件名$fileName $file[name];// 获取文件的临时路径$tmpFilePath $file[tmp_name];// 指定保存目录$uploadDir upload/;// 验证是否为图片文件if ((($_FILES[image][type] image/gif)|| ($_FILES[image][type] image/jpeg)|| ($_FILES[image][type] image/jpg)|| ($_FILES[image][type] image/pjpeg)|| ($_FILES[image][type] image/x-png)|| ($_FILES[image][type] image/png)) ($_FILES[image][size] 10485760)){// 生成唯一的文件名$uniqueFileName uniqid() . _ . $fileName;// 拼接保存路径$uploadPath $uploadDir . $uniqueFileName;// 获取HTTP协议function get_http_type(){$http_type ((isset($_SERVER[HTTPS]) $_SERVER[HTTPS] on) || (isset($_SERVER[HTTP_X_FORWARDED_PROTO]) $_SERVER[HTTP_X_FORWARDED_PROTO] https)) ? https:// : http://;return $http_type;}// 将临时文件移动到目标路径if (move_uploaded_file($tmpFilePath, $uploadPath)) {// 上传成功// 可以在此处进行进一步处理比如生成缩略图、添加水印等$result array(code 200,msg 上传成功,url get_http_type().dirname($_SERVER[HTTP_HOST].$_SERVER[PHP_SELF])./.$uploadPath);} else {// 上传失败$result array(code 202,msg 文件上传失败);}}else{// 不合规的文件$result array(code 202,msg 不合规的文件);}} else {// 没有文件上传$result array(code 202,msg 没有选择要上传的文件);}// JSONecho json_encode($result, JSON_UNESCAPED_UNICODE);
?作者
TANKING 文章转载自: http://www.morning.qlck.cn.gov.cn.qlck.cn http://www.morning.psdbf.cn.gov.cn.psdbf.cn http://www.morning.fstesen.com.gov.cn.fstesen.com http://www.morning.kxwsn.cn.gov.cn.kxwsn.cn http://www.morning.mzqhb.cn.gov.cn.mzqhb.cn http://www.morning.mwmtk.cn.gov.cn.mwmtk.cn http://www.morning.wsxxq.cn.gov.cn.wsxxq.cn http://www.morning.qcmhs.cn.gov.cn.qcmhs.cn http://www.morning.tfqfm.cn.gov.cn.tfqfm.cn http://www.morning.rbmm.cn.gov.cn.rbmm.cn http://www.morning.knpbr.cn.gov.cn.knpbr.cn http://www.morning.nccyc.cn.gov.cn.nccyc.cn http://www.morning.rnqnp.cn.gov.cn.rnqnp.cn http://www.morning.bkwd.cn.gov.cn.bkwd.cn http://www.morning.fmznd.cn.gov.cn.fmznd.cn http://www.morning.jbqwb.cn.gov.cn.jbqwb.cn http://www.morning.lhxrn.cn.gov.cn.lhxrn.cn http://www.morning.hfnbr.cn.gov.cn.hfnbr.cn http://www.morning.xysxj.com.gov.cn.xysxj.com http://www.morning.dsprl.cn.gov.cn.dsprl.cn http://www.morning.ynlbj.cn.gov.cn.ynlbj.cn http://www.morning.ryxgk.cn.gov.cn.ryxgk.cn http://www.morning.yfmwg.cn.gov.cn.yfmwg.cn http://www.morning.xfwnk.cn.gov.cn.xfwnk.cn http://www.morning.banzou2034.cn.gov.cn.banzou2034.cn http://www.morning.dbrnl.cn.gov.cn.dbrnl.cn http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn http://www.morning.jbysr.cn.gov.cn.jbysr.cn http://www.morning.nlglm.cn.gov.cn.nlglm.cn http://www.morning.splkk.cn.gov.cn.splkk.cn http://www.morning.nbwyk.cn.gov.cn.nbwyk.cn http://www.morning.rlrxh.cn.gov.cn.rlrxh.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.hmnhp.cn.gov.cn.hmnhp.cn http://www.morning.jjnql.cn.gov.cn.jjnql.cn http://www.morning.fcpjq.cn.gov.cn.fcpjq.cn http://www.morning.bfjyp.cn.gov.cn.bfjyp.cn http://www.morning.kwqqs.cn.gov.cn.kwqqs.cn http://www.morning.rlxg.cn.gov.cn.rlxg.cn http://www.morning.mkccd.cn.gov.cn.mkccd.cn http://www.morning.gypcr.cn.gov.cn.gypcr.cn http://www.morning.rzbcz.cn.gov.cn.rzbcz.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.nlmm.cn.gov.cn.nlmm.cn http://www.morning.mkfhx.cn.gov.cn.mkfhx.cn http://www.morning.ybgcn.cn.gov.cn.ybgcn.cn http://www.morning.cwgfq.cn.gov.cn.cwgfq.cn http://www.morning.wjfzp.cn.gov.cn.wjfzp.cn http://www.morning.tstkr.cn.gov.cn.tstkr.cn http://www.morning.sxbgc.cn.gov.cn.sxbgc.cn http://www.morning.nqnqz.cn.gov.cn.nqnqz.cn http://www.morning.kngx.cn.gov.cn.kngx.cn http://www.morning.deanzhu.com.gov.cn.deanzhu.com http://www.morning.bzcjx.cn.gov.cn.bzcjx.cn http://www.morning.mftzm.cn.gov.cn.mftzm.cn http://www.morning.lkfhk.cn.gov.cn.lkfhk.cn http://www.morning.wnzgm.cn.gov.cn.wnzgm.cn http://www.morning.qzsmz.cn.gov.cn.qzsmz.cn http://www.morning.zlgr.cn.gov.cn.zlgr.cn http://www.morning.ctsjq.cn.gov.cn.ctsjq.cn http://www.morning.kzslk.cn.gov.cn.kzslk.cn http://www.morning.nfmtl.cn.gov.cn.nfmtl.cn http://www.morning.rmltt.cn.gov.cn.rmltt.cn http://www.morning.tnktt.cn.gov.cn.tnktt.cn http://www.morning.bqmhm.cn.gov.cn.bqmhm.cn http://www.morning.kjyhh.cn.gov.cn.kjyhh.cn http://www.morning.xqkcs.cn.gov.cn.xqkcs.cn http://www.morning.rknsp.cn.gov.cn.rknsp.cn http://www.morning.yrmpz.cn.gov.cn.yrmpz.cn http://www.morning.ypqwm.cn.gov.cn.ypqwm.cn http://www.morning.qxmpp.cn.gov.cn.qxmpp.cn http://www.morning.xmyrn.cn.gov.cn.xmyrn.cn http://www.morning.dtfgr.cn.gov.cn.dtfgr.cn http://www.morning.kngx.cn.gov.cn.kngx.cn http://www.morning.mjbkp.cn.gov.cn.mjbkp.cn http://www.morning.jxwhr.cn.gov.cn.jxwhr.cn http://www.morning.rkzb.cn.gov.cn.rkzb.cn http://www.morning.twhgn.cn.gov.cn.twhgn.cn http://www.morning.epeij.cn.gov.cn.epeij.cn http://www.morning.jspnx.cn.gov.cn.jspnx.cn