標籤:js 外掛程式 uploadify圖片上傳
php Action 伺服器端
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of UploadAction
*
* @author hxwj
*/
class UploadAction extends CommonAction{
public function _initialize(){
//此處為解決Uploadify在Firefox下出現http 302錯誤 重新設定SESSION
$session_name = session_name();
if (isset($_POST[$session_name])) {
session_id($_POST[$session_name]);
session_start();
}
}
public function upload(){
import("ORG.Net.UploadFile");
//匯入上傳類
if($_FILES){
$upload = new UploadFile();
//設定上傳檔案大小
$upload->maxSize = 204800;
//設定上傳檔案類型
$upload->allowExts = array(‘jpg‘, ‘gif‘, ‘png‘, ‘jpeg‘);
//設定附件上傳目錄
$upload->thumb = true;
$upload->thumbPrefix = ‘m_‘; //生產2張縮圖
//設定縮圖最大寬度
$upload->thumbMaxWidth = ‘400,100‘;
//設定縮圖最大高度
$upload->thumbMaxHeight = ‘400,100‘;
$upload->savePath = ‘./Uploads/shunongjj/‘;
//設定需要產生縮圖,僅對影像檔有效
// 設定引用圖片類庫包路徑
//刪除原圖
//$upload->thumbRemoveOrigin = true;
if (!$upload->upload()) {
//捕獲上傳異常
//$this->error($upload->getErrorMsg());
$data[‘status‘]=‘0‘;
$this->ajaxReturn($data,‘json‘);
} else {
//取得成功上傳的檔案資訊
$info = $upload->getUploadFileInfo();
$imgpath = ‘/Uploads/shunongjj/‘.$info[0][‘savename‘];
$data[‘picurl‘] = $imgpath;
$data[‘status‘]=‘100‘;
$this->ajaxReturn($data,‘json‘);
}
}
}
/*
* 無縮圖
*/
public function upload_wsl(){
import("ORG.Net.UploadFile");
//匯入上傳類
if($_FILES){
$upload = new UploadFile();
//設定上傳檔案大小
$upload->maxSize = 204800;
//設定上傳檔案類型
$upload->allowExts = array(‘jpg‘, ‘gif‘, ‘png‘, ‘jpeg‘);
//設定附件上傳目錄
$upload->savePath = ‘./Uploads/shunongjj/‘;
//設定需要產生縮圖,僅對影像檔有效
// 設定引用圖片類庫包路徑
//刪除原圖
//$upload->thumbRemoveOrigin = true;
if (!$upload->upload()) {
//捕獲上傳異常
//$this->error($upload->getErrorMsg());
$data[‘status‘]=‘0‘;
return false;
// $this->ajaxReturn("","上傳失敗","");
} else {
//取得成功上傳的檔案資訊
$info = $upload->getUploadFileInfo();
return $imgpath = ‘/Uploads/shunongjj/‘.$info[0][‘savename‘];
}
}
}
/*
* 公司輪換首頁圖片
*/
public function upload_index(){
import("ORG.Net.UploadFile");
//匯入上傳類
if($_FILES){
$upload = new UploadFile();
//設定上傳檔案大小
$upload->maxSize = 204800;
//設定上傳檔案類型
$upload->allowExts = array(‘jpg‘, ‘gif‘, ‘png‘, ‘jpeg‘);
//設定附件上傳目錄
$upload->savePath = ‘./Uploads/shunongjj/‘;
//設定需要產生縮圖,僅對影像檔有效
// 設定引用圖片類庫包路徑
//刪除原圖
//$upload->thumbRemoveOrigin = true;
if (!$upload->upload()) {
//捕獲上傳異常
//$this->error($upload->getErrorMsg());
$data[‘status‘]=‘0‘;
$this->ajaxReturn($data,‘json‘);
} else {
//取得成功上傳的檔案資訊
$info = $upload->getUploadFileInfo();
$imgpath = ‘/Uploads/shunongjj/‘.$info[0][‘savename‘];
$data[‘picurl‘] = $imgpath;
$data[‘status‘]=‘100‘;
$this->ajaxReturn($data,‘json‘);
}
}
}
}
?>
模板 tpl 端
html
<input id="file_upload" name="file_upload" type="file" />(圖片上傳規範:176*107)
js
<script src="__PUBLIC__/uploadify/jquery.uploadify.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="__PUBLIC__/uploadify/uploadify.css"/>
/**
* uploadify外掛程式上產圖片
*/
$("#file_upload").uploadify({
//指定swf檔案
‘swf‘: ‘__PUBLIC__/uploadify/uploadify.swf‘,
//幕後處理的頁面
‘uploader‘: ‘{:U("Upload/upload")}‘,
//按鈕顯示的文字
‘buttonText‘: ‘上傳圖片‘,
//顯示的高度和寬度,預設 height 30;width 120
//‘height‘: 15,
//‘width‘: 80,
//上傳檔案的類型 預設為所有檔案 ‘All Files‘ ; ‘*.*‘
//在瀏覽視窗底部的檔案類型下拉式功能表中顯示的文本
‘fileTypeDesc‘: ‘Image Files‘,
//允許上傳的檔案尾碼
‘fileTypeExts‘: ‘*.gif; *.jpg; *.png‘,
//發送給背景其他參數通過formData指定
//‘formData‘: { ‘someKey‘: ‘someValue‘, ‘someOtherKey‘: 1 },
//上傳檔案頁面中,你想要用來作為檔案隊列的元素的id, 預設為false 自動產生, 不帶#
//‘queueID‘: ‘fileQueue‘,
//選擇檔案後自動上傳
‘auto‘: true,
//設定為true將允許多檔案上傳
‘onUploadSuccess‘: function(file, data, response){
var data = $.parseJSON(data);
if(data.status==100) {
$("#img_show").html("<img src=‘"+data.picurl+"‘/>");
$("#img_url").val(data.picurl);
}else{
alert("上傳失敗,可能是您上傳的圖片大小不符合規範,請重試!");
}
}
});
});