所以我尝试使用AFNetworking将一个XML文件上传到服务器上 因此,使用他们网站上的示例代码我已经设置了这个。当它运行时,会将某些内容上传到服务器(或者至少它会离开我的计算机)。我可以监控上传,当上传完成后,服务器会识别出它已完成并开始加载文件,但它会加载旧的XML。所以它正确连接到服务器,但我不确定为什么文件上传不能正常工作。另外我只想发送文件,服务器不需要任何标题或参数等 所以我想知道我是否正确存储了数据?或者,如果我没有正确地发送服务器或什么?任何建议都会有所帮助
NSData *iTunesXMLData = [NSData dataWithContentsOfFile:filePath];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
/* NSMutableURLRequest *request =[httpClientmultipartFormRequestWithMethod:@"POST"
path:@"/upload.php?id=5" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:iTunesXMLData name:@"iTunes Music Library" fileName:@"iTunes Music Library.xml" mimeType:@"application/xml"];
}];*/
//I tried this way also, both did the same thing
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload.php?id=5" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFormData:iTunesXMLData name:@"iTunes Music Library"];
}];`
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];`
NSLog(@"Operation: %@", operation);
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation start];
答案 0 :(得分:1)
您是否尝试过捕获操作的成功/失败?在setUploadProgressBlock
:
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// Operation ended successfully
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Something happened!
NSLog(@"ERROR: %@, %@", operation, error);
// Here you can catch operation.responseString to see the response of your server
}];
这是了解服务器返回内容的简便方法。如果有东西上传到您的服务器,请仔细检查您是否收到了正确的文件。 AFAIK,您的AFNetwork似乎没问题。