js单图片上传
<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>
js单图片上传的更多相关文章
- 从web编辑器 UEditor 中单独提取图片上传,包含多图片单图片上传以及在线涂鸦功能
UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码.(抄的...) UEditor是非常好用的富文 ...
- thinkphp 多图片上传 单图片上传
不管是单图片上传还是多图片上传都必须要引用这两个js 下载地址 链接:http://pan.baidu.com/s/1eStkUt0 密码:asvo <script src="Publ ...
- js会员头像上传拖动处理头像类
js会员头像上传拖动处理头像类 点击下载源码文件
- js实现图片上传预览及进度条
原文js实现图片上传预览及进度条 最近在做图片上传的时候,由于产品设计的比较fashion,上网找了比较久还没有现成的,因此自己做了一个,实现的功能如下: 1:去除浏览器<input type= ...
- js实现分段上传文件
使用js实现分段上传文件,本文使用了FileReader对象,可参考:https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader 1)获取文 ...
- 项目总结07:JS图片的上传预览和表单提交(FileReader()方法)
JS图片的上传预览和表单提交(FileReader()方法) 一开始没有搞明白下面这块代码的,今天有时间简单整理下 核心点:FileReader()方法 以下是代码(以JSP文件为例) <!DO ...
- js将用户上传gif动图分解成多张帧图片
js将用户上传gif动图分解成多张帧图片 写在前面 工作中遇到一个这么一个需求:这是一个多图上传的场景,如果用户上传选择多张图片,则上传后直接展示多张图片,如果上传的图片是gif动图,则需要分解这张动 ...
- js获取文件上传进度
js获取文件上传进度: <input name="file" id="FileUpload" type="file" /> &l ...
- js formData图片上传(单图上传、多图上传)后台java
单图上传 <div class="imgUp"> <label>头像单图</label> <input type=&quo ...
随机推荐
- Eclipse连接sqlserver体验过程
以前装的sqlserver 2008试用期到了,就按照网上的步骤,彻底删除了sqlserver,然后又重新装了下,再用eclipse连接的时候,发现提示TCP/IP被禁用,然后找到了sqlserver ...
- js 中文长字符截短&关键字符隐藏 自定义过滤器
两个非常简单的过滤器:隐藏关键字符和字符截短.同样也可以迁移到ng和原生js直接使用(去掉avalon.filters声明即可).后期还有不错的过滤器,还往这里面加 keyword:avalon,js ...
- 抓取远程master分支到本地,并与UI分支合并
1.pull (1)UI: git add . git commit -m git checkout master (2)master: git pull origin master git ...
- Java 性能调优指南之 Java 集合概览
[编者按]本文作者为拥有十年金融软件开发经验的 Mikhail Vorontsov,文章主要概览了所有标准 Java 集合类型.文章系国内 ITOM 管理平台 OneAPM 编译呈现,以下为正文: 本 ...
- J2EE开发环境--RAP
J2EE开发环境--RAP J2EE开发环境分四步: 1.JDK环境 2.tomcat 3.redis环境 4.mysql环境 5.RAP包 线上环境,推荐使用源码,自建应用用户,设置对应规则,禁止关 ...
- hdfs基本操作-python接口
安装hdfs包 pip install hdfs 查看hdfs目录 [root@hadoop hadoop]# hdfs dfs -ls -R / drwxr-xr-x - root supergro ...
- asp.net 對象圖解
- Oracle 18c新特性一览
1. 一般新特性 1.1. Shadow Lost Write Protection Shadow lost write protection检测到一个丢失的写,它会导致一个主要的数据损坏.可以在不需 ...
- Stored Properties 与 Computed Properties
Stored Properties 与 Computed Properties About Swift Stored Properties In its simplest form, a stored ...
- java 规范
https://blog.csdn.net/mengxiangsun/article/details/79020226