直接上代码
前端代码:
上传照片
添加图片
匿名
/*global window, $ */
$(function () {
'use strict';
// Change this to the location of your server-side upload handler:
//var url = "{{ upload_url }}?resourceType=comment_image&resourceId=1";
var url = '/commentUpload';
$('#fileupload').fileupload({
url: url,
dataType: 'json',
// imageMaxWidth:600,
//imageMaxHeight:350,
add: function(e, data) {
var uploadErrors = [];
var acceptFileTypes = /^image\/(gif|jpe?g|png)$/i;
if(data.originalFiles[0]['type'].length && !acceptFileTypes.test(data.originalFiles[0]['type'])) {
uploadErrors.push('请选择图片格式上传');
}
if( data.originalFiles[0]['size'] > 5000000) {
uploadErrors.push('你选择的文件大小超过上传限制');
}
if(uploadErrors.length > 0) {
alert(uploadErrors.join("\n"));
} else {
data.submit();
}
},
done: function (e, data) {
console.log(data.result);
if(parseInt(data.result.error) == 0){
if($("#pic_container li").size()
$("#pic_container").append(
'
'+''
+''
+'
+''
+'
+'
');
}else{
alert("你选择的文件数量超过上传限制");
return false;
}
}
}
}).prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled');
});
后台代码:
$post = array();
$post['resourceType'] ='comment_image';
$post['resourceId'] = 1 ;
$post['getImageInfo'] = 'true' ;
$post['comment_image_file'] = '@'.$_FILES['comment_image_file']['tmp_name'].';filename='.$_FILES['comment_image_file']['name'].';type='.$_FILES['comment_image_file']['type'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,CONFIG::get('api.loupanCommentImageUpload'));
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_exec ($ch);
curl_close ($ch);
使用Curl的方式上传文件