4

我正在使用 AFNetworking 将多部分表单发送到 Web 服务器,但我的AFHTTPRequestOperation. 在我启动它之后,它的成功和失败块永远不会被调用。

这是我的代码(它的简历)

    NSMutableURLRequest *request = [[ServerAPI sharedClient] multipartFormRequestWithMethod:@"POST" path:postUrl parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
        [formData appendPartWithFileData:picture.picture_data name:@"InputFile" fileName:picture.name mimeType:@"image/jpg"];
    }];

    AFHTTPRequestOperation *operation = [[ServerAPI sharedClient] HTTPRequestOperationWithRequest: request success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success");
    } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure");
    }];

    [operation setUploadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        NSLog(@"%f", (totalBytesRead / (float) totalBytesExpectedToRead));
    }];         
    [[ServerAPI sharedClient] enqueueHTTPRequestOperation:operation];

我可以看到进度日志,但从不调用成功和失败块。

picture.picture_data是用 ServerAPINSData初始化的,是 的子类,是单例方法。UIImageJPEGRepresentation(image, 0.7)AFHTTPClientsharedCliend

AFNetworking 不调用我的块的原因是什么,即使没有正确的错误消息?

谢谢你们!

编辑

我在此之前使用相同的 URL 执行了一个获取请求,并且它照常工作。我使用的网址是:part/_layouts/UploadEx.aspx?List=%7BD432BF97-7175-40C1-8E0D-27D8661CBC90%7D&RootFolder=%2Fpwa%2Fpart%2FLibrary&Source=http%3A%2F%2Fwww%2Emysite%2Ecom%2Fpwa%2Fpart%2FLibrary%2FForms%2FAllItems%2Easpx&IsDlg=1

4

1 回答 1

0

在您的代码中,检查您的postUrl. BaseURL+postURL必须有效。尝试使用 URL 使用普通网络浏览器上传图像BaseURL+postURL

编辑

方法HTTPRequestOperationWithRequest:success:failure:不适用于文件上传,但适用于 json/html 获取。尝试使用

AFHTTPRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];

[operation setUploadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        NSLog(@"%f", (totalBytesRead / (float) totalBytesExpectedToRead));
    }];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure");
    }];

[[ServerAPI sharedClient] enqueueHTTPRequestOperation:operation];
于 2013-02-18T09:16:58.120 回答