首页 技术 正文
技术 2022年11月21日
0 收藏 763 点赞 4,521 浏览 1995 个字
<form action="" id="form1">
<input type="file" name="head_img" style="display:none;" id="file0"> //这里一定要写name 后台就是通过name接受内容
<div class="tx upload">
<lable>
{if condition="($company_info.cover_img)"}
<img id="box_headimg" src="{$company_info.cover_img}" alt="">
{else/}
<img id="box_headimg" src="STATIC_BOX/box/images/tx.png" alt="">
{/if}
</lable>
</div>
</form>

前台js

 

$('#file').change(function(){
if (this.files && this.files[0]) { var objUrl = getObjectURL(this.files[0]);
console.log(objUrl);
var data = new FormData($('#form1')[0]);
console.log($('#form1'));
console.log(data); $.ajax({
url: "{:url('advert/upload_img')}",
type: 'POST',
data: data,
async: false,
dataType: 'JSON',
processData: false,
contentType: false, }).done(function(ret){
// alert(ret.path_img);
if(ret.error==1){
$('#cover_img').attr('src',ret.path_img);
$('#cover_img').css('width','100px');
$('#cover_img').css('height','100px');
}else{
alert('上传失败');
}
}); } })

function getObjectURL(file) {            var url = null;
if (window.createObjectURL != undefined) { // basic
url = window.createObjectURL(file);
} else if (window.URL != undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file);
} else if (window.webkitURL != undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file);
}
return url;
}

 

后台php

public function upload(){        $file = request()->file('head_img');        $path=str_replace('\\', '/', 'upload/box');        if (!is_dir($path)) {
mkdir($path, 0777,true);
}
// $path=company_coverimg_path('854'); if($file){
$info = $file->move($path);
if($info){
$path_img ='/'.$path.'/'.$info->getSaveName();
$data['path_img']=$path_img;
$data['error']=1;
}else{
// 上传失败获取错误信息
$msg = $file->getError();
$data['error']=2;
$data['msg']=$msg;
}
} echo json_encode($data); }

<scroll-view scroll-y=”true” style=”height: 200rpx;”>
<view style=”background: red; width: 200px; height: 100px; display: inline-block” ></view>
<view style=”background: green; width: 200px; height: 100px; display: inline-block”></view>
<view style=”background: blue; width: 200px; height: 100px; display: inline-block”></view>
<view style=”background: yellow; width: 200px; height: 100px; display: inline-block”></view>
</scroll-view>

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,036
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,522
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,370
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,151
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,784
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,866