# Niushop开源商城后台上传
---
### **1.引入上传js文件**
```js
<script src="__STATIC__/js/ajax_file_upload.js" type="text/javascript"></script>
<script src="__STATIC__/js/file_upload.js" type="text/javascript"></script>
```
### **2. 前台上传表单,调用上传事件**
```js
<div class="upload-btn-common">
<div>
<input class="input-file" name="file_upload" id="uploadTitleImg" type="file" onchange="imgUpload(this);" title="上传">
<input type="hidden" id="title_img" value="">
</div>
<input type="text" id="text_title_img" class="input-common" readonly="readonly" value="">
<em>上传</em>
<img id="preview_title_img" src="__STATIC__/blue/img/upload-common-select.png" data-src="" data-html="true" data-container="body" data-placement="top" data-trigger="manual" data-original-title="" title="">
</div>
<p class="hint">
建议使用<i class="important-note">宽200</i>像素-<i class="important-note">高60</i>像素内的<i class="important-note">GIF</i>或<i class="important-note">PNG</i>透明图片
</p>
<p class="error">请上传图片</p>
```
```js
function imgUpload(event) {
var fileid = $(event).attr("id");
var data = {};
var id = $(event).next().attr("id");
var only_type = $(event).attr('only-type');
var url = "";
if(!only_type){
data.file_path = "cms_article_img";
url = __URL(ADMINMAIN + '/cms/uploadimage');
}else{
data.file_path = "cms_article_file";
url = __URL(ADMINMAIN + '/cms/uploadcompressedfile');
}
uploadFile({
url: url,
fileId: fileid,
data: data,
callBack: function (res) {
if(res.code){
$("#" + id).val(res.data.path);
$("#text_" + id).val(res.data.path);
$("#preview_"+ id).attr("data-src",__IMG(res.data.path));
showTip(res.message,"success");
}else{
showTip(res.message,"error");
}
}
});
}
```
### **3. 后台配置上传接收控制器方法,后台base控制器定义了不同的上传方法,包括:图片,缩略图,压缩文件,其他文件,视频**
```php
/**
* 图片上传
*/
public function uploadImage()
{
if (!empty($_FILES['file_upload'])) {
$file_path = request()->post("file_path", "common_image");
$upload = new Upload();
$result = $upload->image($_FILES["file_upload"], $file_path);
return json_encode($result);
}
}
/**
* 缩略图上传(根据设置不同的格式)
*/
public function uploadGoodsImage()
{
if (!empty($_FILES['file_upload'])) {
$file_path = request()->post("file_path", "common_image");
$thumb_type = request()->post("thumb_type", "");//缩略图类型 big,mid,small,thumb
$upload = new Upload();
$result = $upload->image($_FILES["file_upload"], $file_path, $thumb_type);
return $result;
}
}
/**
* 压缩包上传
*/
public function uploadCompressedFile()
{
if (!empty($_FILES['file_upload'])) {
$file_path = request()->post("file_path", "common_zip");
$upload = new Upload();
$result = $upload->compressedFiles($_FILES["file_upload"], $file_path);
return json_encode($result);
}
}
/**
* 文件上传
*/
public function uploadFile()
{
if (!empty($_FILES['file_upload'])) {
$file_path = request()->post("file_path", "common_file");
$upload = new Upload();
$result = $upload->file($_FILES["file_upload"], $file_path);
return json_encode($result);
if (request()->isAjax()) {
return $result;
} else {
return json_encode($result);
}
}
}
/**
* 上传到相册
*/
public function imageToAlbum()
{
if (!empty($_FILES['file_upload'])) {
$file_path = request()->post("file_path", "common_album");
$album_id = request()->post("album_id", 0);
$pic_id = request()->post("pic_id", 0);
$thumb_type = request()->post("thumb_type", "");//缩略图类型 big,mid,small,thumb
$upload = new Upload();
$result = $upload->imageToAlbum($_FILES["file_upload"], $file_path, $thumb_type, [ 'album_id' => $album_id, 'pic_id' => $pic_id ]);
if (request()->isAjax()) {
return $result;
} else {
return json_encode($result);
}
}
}
/**
* 视频上传
*/
public function uploadVideo()
{
if (!empty($_FILES['file_upload'])) {
$file_path = request()->post("file_path", "common_album");
$upload = new Upload();
$result = $upload->video($_FILES["file_upload"], $file_path);
if (request()->isAjax()) {
return $result;
} else {
return json_encode($result);
}
}
}
```
```
```