很方便的后台ajax上传文件
<a href="javascript:void(0);" url="{:U('teacherd?id='.$vo['id'])}" class="teacherd" uid="{$vo.id}"><if condition="$vo.spstatus eq 0" >上传批阅<else /> 已批阅 </if></a>
$(".teacherd").click(function () {
var $this=$(this);
var id = $(this).attr("uid");
var url = $(this).attr('url');
var p_id = "{:I('p_id')}";
var display = 'none';
var src = '';
var content = '<div class="margin_left_10" style="margin:20px;"><input type="hidden" name="id" value="' + id + '" ><div class="form-item"><div class="controls"><label class="label">上传文件:<span class="check-tips" id="picture-size"></span></label><input type="file" id="upfile"><input type="hidden" name="photo" id="cover_id_picture_id"/><div class="upload-img-box"><div class="upload-pre-item" style="display:' + display + ';"><img src="' + src + '"/></div></div></div></div></form></div>';
layer.open({
type: ,
title: '上传审批',
content: content,
closeBtn: ,
area: ['350px', '250px'],
btn: '确定',
yes: function () {
var fd = new FormData();
var uname = $('#upfile').val().split('.'); //获取上传文件名并拆封成数组(获取后缀)
if((uname[] != 'docx') && ( uname[] != 'doc')){
alert(uname[]);
return false;
}
fd.append("upload", );
fd.append("id", id); //将id传入
fd.append("p_id",p_id); //p_id
fd.append("upfile", $("#upfile").get().files[]);
$.ajax({
url: "{:U('uploadss')}",
type: "POST",
processData: false,
contentType: false,
data: fd,
success: function (d) {
if(d.code == ){
$(".layui-layer-shade").hide();
$(".layui-layer-page").hide();
$(".bac").fadeIn();
$(".bac1").html("上传成功")
$this.html("已审批")
}else{
$("#layui-layer-shade1").hide();
$("#layui-layer1").hide();
$(".bac").fadeIn();
$(".bac1").html("上传失败")
}
// console.log(d);
}
})
}
})
});
$(".closeA").on("click",function(){
$(".bac").fadeOut()
})
$(".layui-layer-btn0").on("click",function(){
$(".layui-layer-shade").hide();
$(".layui-layer-page").hide();
})
后台代码:
/*
* 上传审批
* Carey
*/ public function uploadss() {
$id = $_POST['id']; //id
$p_id = $_POST['p_id']; $exam_listsDB = M('exam_lists');
$username = $exam_listsDB->where(array('id' => $id))->getField('username'); $username = iconv("UTF-8", "gb2312", $username); //转码utf8
$saveName = $username . '_' . $p_id . '_' . $id; //文件命名规则考场p_id_用户id; $rootpath = '../Public/Uploads/examsp/kc' . $p_id . '/';
mkdir($rootpath, ); $upload = new \Think\Upload(); // 实例化上传类
$upload->maxSize = ; // 设置附件上传大小 3M
$upload->exts = array('application/msword', 'doc', 'docx'); // 设置附件上传类型
$upload->rootPath = $rootpath; // 设置附件上传根目录 // 上传文件
$upload->autoSub = TRUE; //自动生成子目录
$upload->subName = 'py';
$upload->saveName = $saveName; //根据p_id-id
$upload->replace = TRUE; //存在同名是否覆盖 true覆盖 $info = $upload->uploadOne($_FILES['upfile']); if (!$info) {// 上传错误提示错误信息
$redata = array(
'code' => , 'info' => $info, 'files' => $_FILES, 'id' => $id, 'p_id' => $p_id, 'username' => $username,
);
} else {
// 上传成功
$info['savename'] = iconv("gb2312", "UTF-8", $info['savename']); //转码utf8
$cha_dourl['cha_dourl'] = 'Public/Uploads/examsp/' . $info['savepath'] . $info['savename'];
$cha_dourl['sptime'] = time();
$cha_dourl['spstatus'] = ;
/* 如果上传成功更新表单数据 */
$upda = $exam_listsDB->where(array('id' => $id))->save($cha_dourl);
if ($upda) {
$redata = array(
'code' => , 'info' => $info, 'files' => $_FILES, 'id' => $id, 'p_id' => $p_id, 'upda' => $upda,
);
} else {
$redata = array(
'code' => , 'info' => $info, 'files' => $_FILES, 'id' => $id, 'p_id' => $p_id, 'upda' => $upda,
);
}
}
$this->ajaxReturn($redata);
}
很方便的后台ajax上传文件的更多相关文章
- 闲话ajax,例ajax轮询,ajax上传文件[开发篇]
引语:ajax这门技术早已见怪不怪了,我本人也只是就自己真实的经验去总结一些不足道的话.供不是特别了解的朋友参考参考! 本来早就想写一篇关于ajax的文章的,但是前段时间一直很忙,就搁置了,趁着元旦放 ...
- atitit.ajax上传文件的实现原理 与设计
atitit.ajax上传文件的实现原理 与设计 1. 上传文件的三大难题 1 1.1. 本地预览 1 1.2. 无刷新 1 1.3. 进度显示 1 2. 传统的html4 + ajax 是无法直 ...
- IE8/9 JQuery.Ajax 上传文件无效
IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...
- 伪ajax上传文件
伪ajax上传文件 最近在折腾伪ajax异步上传文件. 网上搜索了一下,发现大部分方法的input file控件都局限于form中,如果是在form外的呢? 必须动态生成一个临时form和临时if ...
- Ajax 上传文件(input file FormData)
FormData对象用以将数据编译成键值对,以便用XMLHttpRequest来发送数据.其主要用于发送表单数据,但亦可用于发送带键数据(keyed data),而独立于表单使用. jQuery Aj ...
- django系列6--Ajax05 请求头ContentType, 使用Ajax上传文件
一.请求头ContentType ContentType指的是请求体的编码类型,常见的类型共有三种: 1.application/x-www-form-urlencoded 这应该是最常见的 POST ...
- Ajax上传文件/照片时报错TypeError :Illegal invocation
问题 Ajax上传文件/照片时报错TypeError :Illegal invocation 解决 网上搜索问题,错误原因可能有以下几个,依次检查: 请求类型有误,如post请求,但在后台设置的是ge ...
- django上课笔记7-jQuery Ajax 和 原生Ajax-伪造的Ajax-三种Ajax上传文件方法-JSONP和CORS跨域资源共享
一.jQuery Ajax 和 原生Ajax from django.conf.urls import url from django.contrib import admin from app01 ...
- jS Ajax 上传文件报错"Uncaught TypeError: Illegal invocation"
使用jquery ajax异步提交文件的时候报Uncaught TypeError :Illegal invocation错误,报错信息如图: 错误原因: jQuery Ajax 上传文件处理方式,使 ...
随机推荐
- javaweb项目jsp跳转servlet Error instantiating servlet class 问题
问题: HTTP Status 500 - Error instantiating servlet class RecommenderServlet type Exception report mes ...
- jquery 元素控制(追加元素/追加内容)
参考网址:http://www.jquerycn.cn/a_5521 一.在元素内部/外部追加元素 append,prepend:添加到子元素 before,after:作为兄弟元素添加 html: ...
- Oracle索引梳理系列(九)- 浅谈聚簇因子对索引使用的影响及优化方法
版权声明:本文发布于http://www.cnblogs.com/yumiko/,版权由Yumiko_sunny所有,欢迎转载.转载时,请在文章明显位置注明原文链接.若在未经作者同意的情况下,将本文内 ...
- python(一)
python数学函数 abs(x) 返回数字的绝对值,如abs(-10) 返回 10 ceil(x) 返回数字的上入整数,如math.ceil(4.1) 返回 5 cmp(x, y) 如果 x < ...
- excel基本
1,换行:control+option(alt)+enter
- play with usb
1) struct usbdevfs_bulktransfer { unsigned int ep; unsigned int len; unsigned i ...
- [LeetCode] Paint House 粉刷房子
There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Linux 中 SVN 重启关闭
a.启动SVN svnserve -d -r /A/B/svn/ 其中 -d 表示守护进程, -r 表示在后台执行 /A/B/svn/ 为svn的安装目录 b.关闭SVN 这里采取linux杀死进程的 ...
- SQLite3源程序分析之查询处理及优化
前言 查询处理及优化是关系数据库得以流行的根本原因,也是关系数据库系统最核心的技术之一.SQLite的查询处理模块很精致,而且很容易移植到不支持SQL的存储引擎(Berkeley DB最新的版本已经将 ...