【问题标题】:AFNetworking file upload failsAFNetworking 文件上传失败
【发布时间】:2014-09-05 09:17:47
【问题描述】:

您好,我正在使用以下代码在服务器上上传图片

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];

我收到以下错误

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)

我已尝试在另一台服务器上控制达到成功阻止但没有文件上传到服务器。

文件上传需要什么服务器配置或权限?

我正在使用 iOS 7。

【问题讨论】:

标签: ios objective-c ios7 file-upload afnetworking


【解决方案1】:
 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);
}];

它为我工作。希望对你有帮助

【讨论】:

  • 我会试试的,会告诉你的。
  • 我得到以下错误使用上面的代码失败错误域=com.alamofire.error.serialization.response Code=-1011“请求失败:禁止(403)”用户信息=0xaea3bf0 {com.alamofire. serialization.response.error.response= 403 Forbidden

    Forbidden

    你没有权限访问此服务器上的 /upload_test/。

【解决方案2】:

这个错误来自我们使用 HTTP 代码发送数据的 API,所以请在 API 代码中说明,如果他们正在发送数据,那么 HTTP 代码应该是 200

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 2012-03-18
    相关资源
    最近更新 更多