Ios AFN网络文件上载失败
嗨,我正在使用以下代码在服务器上上传图像Ios AFN网络文件上载失败,ios,objective-c,ios7,file-upload,afnetworking,Ios,Objective C,Ios7,File Upload,Afnetworking,嗨,我正在使用以下代码在服务器上上传图像 AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer]; NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:@"http://test.com/upload_test/" parameters:nil const
AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
NSMutableURLRequest *request =
[serializer multipartFormRequestWithMethod:@"POST" URLString:@"http://test.com/upload_test/" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:dataToPost
name:@"attachment"
fileName:@"myimage.png"
mimeType:@"image/png"];
} error:nil];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
AFHTTPRequestOperation *operation =
[manager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure %@", error.description);
}];
[operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
long long totalBytesWritten,
long long totalBytesExpectedToWrite) {
NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation start];
AFHTTPRequestSerializer*serializer=[AFHTTPRequestSerializer serializer];
NSMutableURLRequest*请求=
[序列化程序multipartFormRequestWithMethod:@“POST”URLString:@”http://test.com/upload_test/“参数:nil constructingBodyWithBlock:^(id formData){
[formData appendPartWithFileData:dataToPost
姓名:@“附件”
文件名:@“myimage.png”
mimeType:@“image/png”];
}错误:无];
AFHTTPRequestOperationManager*manager=[AFHTTPRequestOperationManager];
manager.responseSerializer=[AFHTTPResponseSerializer序列化程序];
AFHTTPRequestOperation*操作=
[manager HTTPRequestOperationWithRequest:请求
成功:^(AFHTTPRequestOperation*操作,id响应对象){
NSLog(@“成功%@”,响应对象);
}失败:^(AFHTTPRequestOperation*操作,NSError*错误){
NSLog(@“故障%@”,错误描述);
}];
[operation setUploadProgressBlock:^(nsInteger uu未使用的字节写入,
写了很长很长的TotalBytes,
long long totalBytesExpectedToWrite){
NSLog(@“写入%lld/%lld”、TotalBytesWrite、totalBytesExpectedToWrite);
}];
[操作启动];
我得到了以下错误
Failure Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: not found (404)" UserInfo=0xc0428a0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0xaa389e0> { URL: http://test.com/upload_test/ } { status code: 404, headers {
Connection = "Keep-Alive";
"Content-Length" = 210;
"Content-Type" = "text/html; charset=iso-8859-1";
Date = "Fri, 05 Sep 2014 07:32:49 GMT";
"Keep-Alive" = "timeout=5, max=100";
Server = "Apache/2.2.15 (Win32) PHP/5.3.5";
} }, NSErrorFailingURLKey=http://test.com/upload_test/, NSLocalizedDescription=Request failed: not found (404)
Failure Error Domain=com.alamofire.Error.serialization.response code=-1011“请求失败:未找到(404)”UserInfo=0xc0428a0{com.alamofire.serialization.response.Error.response={URL:http://test.com/upload_test/ }{状态代码:404,标题{
连接=“保持活动”;
“内容长度”=210;
“内容类型”=“文本/html;字符集=iso-8859-1”;
日期=“2014年9月5日星期五07:32:49 GMT”;
“保持活动”=“超时=5,最大=100”;
Server=“Apache/2.2.15(Win32)PHP/5.3.5”;
}},NSErrorFailingURLKey=http://test.com/upload_test/,NSLocalizedDescription=请求失败:未找到(404)
我在另一台服务器上进行了尝试,该服务器上的控件达到成功块,但没有文件上载到该服务器。
上传文件需要什么服务器配置或权限?
我正在使用iOS 7。AFHTTPRequestOperationManager*manager=[[AFHTTPRequestOperationManager alloc]initWithBaseURL:[nsurlurlwithstring:@“您的服务器基本url]”;
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"Your server base url"]];
NSData *imageData = UIImagePNGRepresentation(image);
[manager POST:@"url to upload image" parameters:@"parameter required by the server" constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"file" fileName:@"image.png" mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure %@, %@", error, operation.responseString);
}];
NSData*imageData=UIImagePNGRepresentation(图像);
[管理员帖子:@“上传图像的url”参数:@“服务器所需的参数”constructingBodyWithBlock:^(id formData){
[formData appendPartWithFileData:imageData名称:@“file”文件名:@“image.png”mimeType:@“image/png”];
}成功:^(AFHTTPRequestOperation*操作,id响应对象){
NSLog(@“成功%@”,响应对象);
}失败:^(AFHTTPRequestOperation*操作,NSError*错误){
NSLog(@“故障%@,%@”,错误,操作。响应字符串);
}];
这对我很有用。我希望它能有所帮助。这个错误来自于我们使用HTTP代码发送数据的API,因此请输入API代码,如果他们发送数据,那么HTTP代码应该总是200检查这个链接@Ganesh我已经尝试过那个链接。名称:@“附件”文件名:@“myimage.png”mimeType:@“image/png”对于此参数,服务器是否需要创建这些参数,或者它是默认属性?您可以查看此问题,我将尝试它,并让您知道。我使用上面的代码失败得到以下错误:error Domain=com.alamofire.error.serialization.response code=-1011“请求失败:禁止(403)”UserInfo=0xaea3bf0{com.alamofire.serialization.response.error.response=我需要发送哪些参数??需要任何其他服务器配置??我认为您的api需要一些令牌或身份验证才能访问