thinkphp ajaxfileupload實(shí)現(xiàn)異步上傳圖片的示例
thinkphp開發(fā)圖片上傳,圖片異步上傳是目前比較方便的功能,這里我就不寫css文件了,將代碼寫出來。引入核心文件下載https://github.com/carlcarl/A...
HTML
下面首先在html頁面引入相關(guān)js資源
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>圖片上傳</title> <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script> <script type="text/javascript" src="js/ajaxfileupload.js"></script> </head> <body> </body> </html>
接下來在body中創(chuàng)建相關(guān)div
<label class="title w100">封面圖片:</label> <div class="f_l"> <label class="fileupload" onclick="upd_file(this,'image_file');"> <input type="file" class="filebox" name="image_file" id="image_file"/> <!--上傳成功后圖片會(huì)給value賦值圖片路徑,以便于form表單提交數(shù)據(jù)--> <input type="hidden" name="image" value=""> </label> <label class="fileuploading hide" ></label> </div> <div class="blank15"></div> <!--上傳成功后圖片會(huì)在這里顯示否則是默認(rèn)圖片--> <img id="image" src="/Public/images/empty_thumb.gif" />
解釋一下:
其中upd_file(this,'image_file')不可缺少
其中隱藏的input 是用于上傳成功后賦值圖片路徑,以便于form表單提交數(shù)據(jù)
接下來在html中編輯javascript腳本以便于傳遞和提交圖片功能
<script>
function upd_file(obj,file_id){
$("input[name='"+file_id+"']").bind("change",function(){
$(obj).hide();
$(obj).parent().find(".fileuploading").removeClass("hide");
$(obj).parent().find(".fileuploading").removeClass("show");
$(obj).parent().find(".fileuploading").addClass("show");
$.ajaxFileUpload
(
{
url:'/index.php/home/avatar/app_upload_image',//上傳圖片處理文件
secureuri:false,
fileElementId:file_id,
dataType: 'json',
success: function (data, status)
{
$(obj).show();
$(obj).parent().find(".fileuploading").removeClass("hide");
$(obj).parent().find(".fileuploading").removeClass("show");
$(obj).parent().find(".fileuploading").addClass("hide");
if(data.status==1)
{
$("#image").attr("src",data.thumb_url+"?r="+Math.random());
$("input[name='image']").val(data.url);//返回json后將隱藏input賦值
//$("#img_url").html('<input type="hidden" name="img_url" value="'+ path.path +'" />');
}
else
{
$.showErr(data.msg);
}
},
error: function (data, status, e)
{
$.showErr(data.responseText);;
$(obj).show();
$(obj).parent().find(".fileuploading").removeClass("hide");
$(obj).parent().find(".fileuploading").removeClass("show");
$(obj).parent().find(".fileuploading").addClass("hide");
}
}
);
$("input[name='"+file_id+"']").unbind("change");
});
}
<script>
thikphp 中創(chuàng)建方法 app_upload_image()
function app_upload_image($maxSize=52428800){
$id=session('id');
$config=array(
'rootPath' =>'Upload', //文件上傳保存的根路徑
'savePath' =>'/avatar/',
'exts' => array('jpg', 'gif', 'png', 'jpeg','bmp'),
'maxSize' => $maxSize,
'autoSub' => true,
);
$upload = new \Think\Upload($config);// 實(shí)例化上傳類
$z = $upload->uploadOne($_FILES['image_file']);
if($z) {
//拼接圖片的路徑名
$img='/Upload'.$z['savepath'].$z['savename'];
$_POST['image_file']=$img;
//獲取上傳圖片絕對(duì)路徑
$imgsrc=$_SERVER['DOCUMENT_ROOT'].__ROOT__.$_POST['image_file'];
$image = new \Think\Image();
$image->open($imgsrc);
//將圖片裁剪為400x400并保存為corp.jpg
$image->thumb(205, 160,\Think\Image::IMAGE_THUMB_CENTER)->save($imgsrc);
$this->ajaxReturn(array("thumb_url"=>$img,"url"=>$img,"status"=>1));
}
}
OK這樣就好了,首先和大家說一下,如果ajaxfileupload.js報(bào)錯(cuò)程序是不會(huì)跑通的,如果你的程序報(bào)錯(cuò)就檢查你的ajaxfileupload文件是不是版本的問題
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- TP3.2.3框架使用CKeditor編輯器在頁面中上傳圖片的方法分析
- thinkphp3.2實(shí)現(xiàn)上傳圖片的控制器方法
- thinkPHP利用ajax異步上傳圖片并顯示、刪除的示例
- thinkphp5上傳圖片及生成縮略圖公共方法(分享)
- thinkPHP實(shí)現(xiàn)上傳圖片及生成縮略圖功能示例
- 使用ThinkPHP+Uploadify實(shí)現(xiàn)圖片上傳功能
- ThinkPHP實(shí)現(xiàn)圖片上傳操作的方法詳解
- thinkphp jquery實(shí)現(xiàn)圖片上傳和預(yù)覽效果
- 基于ThinkPHP5.0實(shí)現(xiàn)圖片上傳插件
- ThinkPHP5+Layui實(shí)現(xiàn)圖片上傳加預(yù)覽功能
- ThinkPHP5.0 圖片上傳生成縮略圖實(shí)例代碼說明
- TP框架實(shí)現(xiàn)上傳一張圖片和批量上傳圖片的方法分析
相關(guān)文章
laravel5.6 框架操作數(shù)據(jù) Eloquent ORM用法示例
這篇文章主要介紹了laravel5.6 框架操作數(shù)據(jù) Eloquent ORM用法,結(jié)合實(shí)例形式詳細(xì)分析了laravel5.6 框架使用Eloquent ORM操作數(shù)據(jù)增刪改查相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2020-01-01
Zend Framework開發(fā)入門經(jīng)典教程
這篇文章主要介紹了Zend Framework開發(fā)入門知識(shí)點(diǎn),詳細(xì)介紹了Zend Framework開發(fā)的zend源碼下載,環(huán)境配置,基本組件使用與相關(guān)注意事項(xiàng)等,需要的朋友可以參考下2016-03-03
網(wǎng)頁游戲開發(fā)入門教程三(簡單程序應(yīng)用)
用哪種組合,真的不重要。重要的是時(shí)間和成本。復(fù)雜的地方在數(shù)據(jù)的交互和完善,而不在技術(shù)或效果的實(shí)現(xiàn)。2009-11-11
php的array數(shù)組和使用實(shí)例簡明教程(容易理解)
最近在教一個(gè)朋友php,他沒有其他語言的基礎(chǔ)。對(duì)array的理解和用法有些模糊。所以寫了個(gè)教程,需要的朋友可以參考下2014-03-03
解決PHPstudy Apache無法啟動(dòng)的問題【親測有效】
這篇文章主要介紹了PHPstudy Apache無法啟動(dòng)的問題及解決方法【親測有效】,本文給大家總結(jié)了三種方法供大家參考,需要的朋友可以參考下2020-10-10
數(shù)組任意位置插入元素,刪除特定元素的實(shí)例
下面小編就為大家?guī)硪黄獢?shù)組任意位置插入元素,刪除特定元素的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03

