ios ASIFormDataRequest上传图片到php服务器
2013-09-28 17:23
323 查看
需要ASIHTTPRequest第三方库的童鞋请到我的资源下载
使用ASIHTTPRequest第三方库需要导入系统包如下:
libz.1.2.5.dylib ,
MobileCoreServices.framework ,
SystemConfiguration.framework ,
CFNetwork.framework ,
CoreGraphics.framework
需要导入ASIHTTPRequest包里的ASIFormDataRequest.h
#import "ASIFormDataRequest.h"
//上传图片
-(void)uploadImages:(UIButton *)sender
{
NSData *data =
UIImagePNGRepresentation(self.img.image);
NSURL *url = [[NSURL
alloc]initWithString:@"http://127.0.0.1/uploadFile/upload.php"];
//以表格形式的请求对象
ASIFormDataRequest *request = [[ASIFormDataRequest
alloc]initWithURL:url];
request.delegate =self;
request.requestMethod =
@"POST";//设置请求方式
//添加请求内容
[request addData:data
withFileName:[NSString
stringWithFormat:@"%d.png",arc4random()]
andContentType:@"image/png"
forKey:@"file"];
//开始异步请求
[request startAsynchronous];
//如果成功则自动执行
[request
setDidFinishSelector:@selector(requestedSuccessfully)];
//如果失败则自动执行
[request
setDidFailSelector:@selector(requestedFail)];
}
-(void)requestedSuccessfully
{
UIAlertView *alert = [[UIAlertView
alloc]initWithTitle:@"提示"
message:@"图片上传成功!" delegate:nil
cancelButtonTitle:@"确定" otherButtonTitles:
nil];
[alert
show];
}
-(void)requestedFail
{
UIAlertView *alert = [[UIAlertView
alloc]initWithTitle:@"提示"
message:@"图片上传失败!" delegate:nil
cancelButtonTitle:@"确定" otherButtonTitles:
nil];
[alert
show];
}
使用的php文件 upload.php
<?php
header("Content-type:text/html;charset=utf-8");
print_r($_FILES['file']);
$filename = $_FILES['file']['name'];
if(!$_FILES['file']['error']){
if(move_uploaded_file($_FILES['file']['tmp_name'],"./upload/".$filename))
{
echo "文件上传成功";
}else{
echo "文件上传失败le";
}
}else{
echo "文件上传错误";
}
?>
使用ASIHTTPRequest第三方库需要导入系统包如下:
libz.1.2.5.dylib ,
MobileCoreServices.framework ,
SystemConfiguration.framework ,
CFNetwork.framework ,
CoreGraphics.framework
需要导入ASIHTTPRequest包里的ASIFormDataRequest.h
#import "ASIFormDataRequest.h"
//上传图片
-(void)uploadImages:(UIButton *)sender
{
NSData *data =
UIImagePNGRepresentation(self.img.image);
NSURL *url = [[NSURL
alloc]initWithString:@"http://127.0.0.1/uploadFile/upload.php"];
//以表格形式的请求对象
ASIFormDataRequest *request = [[ASIFormDataRequest
alloc]initWithURL:url];
request.delegate =self;
request.requestMethod =
@"POST";//设置请求方式
//添加请求内容
[request addData:data
withFileName:[NSString
stringWithFormat:@"%d.png",arc4random()]
andContentType:@"image/png"
forKey:@"file"];
//开始异步请求
[request startAsynchronous];
//如果成功则自动执行
[request
setDidFinishSelector:@selector(requestedSuccessfully)];
//如果失败则自动执行
[request
setDidFailSelector:@selector(requestedFail)];
}
-(void)requestedSuccessfully
{
UIAlertView *alert = [[UIAlertView
alloc]initWithTitle:@"提示"
message:@"图片上传成功!" delegate:nil
cancelButtonTitle:@"确定" otherButtonTitles:
nil];
[alert
show];
}
-(void)requestedFail
{
UIAlertView *alert = [[UIAlertView
alloc]initWithTitle:@"提示"
message:@"图片上传失败!" delegate:nil
cancelButtonTitle:@"确定" otherButtonTitles:
nil];
[alert
show];
}
使用的php文件 upload.php
<?php
header("Content-type:text/html;charset=utf-8");
print_r($_FILES['file']);
$filename = $_FILES['file']['name'];
if(!$_FILES['file']['error']){
if(move_uploaded_file($_FILES['file']['tmp_name'],"./upload/".$filename))
{
echo "文件上传成功";
}else{
echo "文件上传失败le";
}
}else{
echo "文件上传错误";
}
?>
相关文章推荐
- [iOS]服务器使用php时多图片上传AFNetworking的设置
- PHP POST接收处理 IOS上传NSData图片数据,上传图片到服务器
- ios ASIFormDataRequest上传图片到php服务器
- iOS 上传图片到服务器
- PHP实现微信图片上传到服务器的方法示例
- PHP接收Android和ios上传的图片,支持多图片
- iOS图片上传服务器(ASIHTTPRequest,SpringMVC)
- iOS -- 摄像头/相册获取图片,压缩图片,上传服务器
- iOS -- 上传图片到服务器
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法
- iOS上传图像到服务器,以及服务器PHP接收的几种方法
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- iOSAFN上传图片 php接收(一)(模拟表单上传)
- PHP-postman上传图片到服务器
- android 打开系统相册得到路径 上传图片到php服务器
- android 图片上传java,php服务器
- ios文件,图片上传服务器
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios表单提交(多个参数)数据到服务器,上传图片AFNetWorking
- iOS上传图片到服务器