tp端
public function uploadimg()
{
if (IS_POST) {
$foo = $_POST['foo'];
if ($_FILES) {
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 3145728;// 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');
// 允许上传的文件类型(留空为不限制),使用数组或者逗号分隔的字符串设置,默认为空
$upload->mimes = '';
$upload->rootPath = './Uploads/';
// 设置附件上传根目录
$upload->savePath = '';
// 设置上传的子目录
$upload->saveName = 'uniqid';
// 上传文件的保存规则,支持数组和字符串方式定义
$upload->saveExt = '';
// 上传文件的保存后缀,不设置的话使用原文件后缀
$upload->replace = true;
// 存在同名文件是否是覆盖,默认为false
$upload->autoSub = true;
// 自动使用子目录保存上传文件 默认为true
$upload->subName = array('date', 'Ymd');
// 子目录创建方式,采用数组或者字符串方式定义
$upload->hash = true;
// 是否生成文件的hash编码 默认为true
// 上传文件
$info = $upload->upload();
if (!$info) {// 上传错误提示错误信息
$chuantu['code'] = "10010"; //失败
$this->ajaxReturn($chuantu); //图片上传失败
} else {// 上传成功
$chuantu['code'] = "200"; //失败
$this->ajaxReturn($chuantu); //图片上传失败
}
}
$chuantu['code'] = "200"; //失败
$this->ajaxReturn($chuantu); //图片上传失败
}
$this->display();
}
IOS端
// 1.获得请求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
mgr.requestSerializer = [AFJSONRequestSerializer serializer];
mgr.responseSerializer = [AFJSONResponseSerializer serializer];
mgr.responseSerializer.acceptableContentTypes = [NSSet setWithob
// 2.发送请求(做文件上传)
// parameters : 只能放非文件参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"foo"] = @"bar";
[mgr POST:KGENURL@"/Advertising/uploadimg" parameters:params
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// 一定要在这个block中添加文件参数
// 加载文件数据
NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:@"shanguangdeng"], 0.0001);
DLog(@"%@",data);
// 拼接文件参数
[formData appendPartWithFileData:data name:@"file" fileName:@"" mimeType:@"image/jpeg"];
}
success:^(AFHTTPRequestOperation *operation, id responseob
NSLog(@"上传成功----%@", responseob
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"上传失败----%@", error);
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}];
最佳答案