base64多图上传到后台

东西/原料
- sublime
- thinkphp
方式/步调
- 1
起首建立html页面:
<div class="container">
<label>请选择一个图像文件:</label>
<button style="display:none;" id="select">(从头)选择图片</button>
<button id="add">选择上传图片</button>
<input type="file" id="file_input" multiple/> <!--用input标签并选择type=file,记得带上multiple,否则就只能单选图片了-->
<button id="submit">提交</button>
</div>
- 2
<style type="text/css">
.float{
float:left;
width : 200px;
height: 200px;
overflow: hidden;
border: 1px solid #CCCCCC;
border-radius: 10px;
padding: 5px;
margin: 5px;
}
img{
position: relative;
}
.result{
width: 200px;
height: 200px;
text-align: center;
box-sizing: border-box;
}
#file_input{
display: none;
}
</style>
- 3
window.onload = function(){
var input = document.getElementById("file_input");
var result;
var dataArr = []; // 储存所选图片的成果(文件名和base64数据)
var fd; //FormData体例发送请求
var oSelect = document.getElementById("select");
var oAdd = document.getElementById("add");
var oSubmit = document.getElementById("submit");
var oInput = document.getElementById("file_input");
if(typeof FileReader==='undefined'){
alert("抱愧,你的浏览器不撑持 FileReader");
input.setAttribute('disabled','disabled');
}else{
input.addEventListener('change',readFile,false);
}
function readFile(){
fd = new FormData();
var iLen = this.files.length;
var index = 0;
for(var i=0;i<iLen;i++){
if (!input['value'].match(/.jpg|.gif|.png|.jpeg|.bmp/i)){ //判定上传文件格局
return alert("上传的图片格局不准确,请从头选择");
}
var reader = new FileReader();
reader.index = i;
fd.append(i,this.files[i]);
reader.readAsDataURL(this.files[i]); //转当作base64
reader.onload = function(e){
var imgMsg = this.result ;
dataArr.push(imgMsg);
result = '<div class="result"><img src="'+this.result+'" alt=""/></div>';
var div = document.createElement('div');
div.innerHTML = result;
div['className'] = 'float';
div['index'] = index;
document.getElementsByTagName('body')[0].appendChild(div); //插入dom树
var img = div.getElementsByTagName('img')[0];
img.onload = function(){
var nowHeight = ReSizePic(this); //设置图片巨细
this.parentNode.style.display = 'block';
var oParent = this.parentNode;
if(nowHeight){
oParent.style.paddingTop = (oParent.offsetHeight - nowHeight)/2 + 'px';
}
}
index++;
}
}
}
function send(){
var submitArr = [];
for (var i = 0; i < dataArr.length; i++) {
if (dataArr[i]) {
submitArr.push(dataArr[i]);
}
}
$.ajax({
url : '/home/test/upload',
type : 'post',
data : {"image":dataArr},
dataType: 'json',
success : function(data){
if (data == 1) {
alert("图片上传当作功");
}else{
alert("图片上传掉败");
}
}
})
}
oSelect.onclick=function(){
oInput.value = ""; // 先将oInput值清空,不然选择图片与前次不异时change事务不会触发
$('.float').remove(); //清空已选图片
dataArr = [];
index = 0;
oInput.click();
}
oAdd.onclick=function(){
oInput.value = ""; // 先将oInput值清空,不然选择图片与前次不异时change事务不会触发
oInput.click();
}
oSubmit.onclick=function(){
if(!dataArr.length){
return alert('请先选择文件');
}
send();
}
}
- 4
function ReSizePic(ThisPic) {
var RePicWidth = 200; //这里点窜为您想显示的宽度值
var TrueWidth = ThisPic.width; //图片现实宽度
var TrueHeight = ThisPic.height; //图片现实高度
if(TrueWidth>TrueHeight){
var reWidth = RePicWidth;//广大于高
ThisPic.width = reWidth;
var nowHeight = TrueHeight * (reWidth/TrueWidth); //垂直居中
return nowHeight; //将图片点窜后的高度返回,供垂直居顶用
}else{
var reHeight = RePicWidth; //宽小于高
ThisPic.height = reHeight;
}
}
- 5
后台数据处置:
public function upload(){
foreach ($_POST['image'] as $key => $value) {
$up_dir = './upload/';//存放在当前目次的upload文件夹下
if (!file_exists($up_dir)) {
mkdir($up_dir,0777);
}
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $value,$result)) {
$type = $result[2];
if (in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))) {
$new_file = $up_dir.date('YmdHis_').rand(10000,99999).'.'.$type;
if (file_put_contents($new_file, base64_decode(str_replace($result[1],'',$value)))) {
$img_path = str_replace('../../..', '', $new_file);
$re[] += 1;
}else{
$re[] += 0;
}
}else{
$re[] += 0;
}
}else{
$re[] += 0;
}
}
if (count($re) == count($_POST['image'])) {
echo 1;
}else{
echo 0;
}
}
执行过程及成果
- 1
起首选择图片上传
- 2
测试上传一张图片,点击提交
- 3
图片已当作功保留在文件夹中
- 4
选择多张图片,点击提交
- 5
多张图片已当作功保留在文件夹中
注重事项
- 注重图片保留路径问题
- 注重base64转换问题
来源:百闻(微信/QQ号:9397569),转载请保留出处和链接!
本文链接:https://www.ibaiwen.com/web/223283.html
- 上一篇: xshell怎么下载,xshell下载方法
- 下一篇: 群宝库怎如何发付费贴
- 热门文章
-
WB蒙特利尔(WB Montreal)——欧美十大最差视频游戏开发商
迅猛龙(Velociraptor)——欧美史前十大死亡动物
什么是果酱猫(What Marmalade Cats)?
神奇蜘蛛侠2(The Amazing Spider-Man 2)——欧美最佳蜘蛛侠电影
希瑟(Heather)——欧美十大最佳柯南灰歌
二人梭哈
faceu激萌怎么把瘦脸开到最大
奥兹奥斯本(Ozzy Osbourne)——欧美十大高估歌手
什么是小脑前下动脉(Anterior Inferior Cerebellar Artery)?
我应该知道康涅狄格州的什么(What Should I Know About Connecticut)?
- 热评文章
- 最新评论
-
- 最近访客
-
- 站点信息
-
- 文章总数:200248
- 页面总数:9
- 分类总数:1
- 标签总数:0
- 评论总数:0
- 浏览总数:497