使用AFNetworking通过POST上传文件 [英] Uploading a file with POST using AFNetworking

查看:172
本文介绍了使用AFNetworking通过POST上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图使用AFNetworking通过POST将XML文件上传到服务器。

所以使用他们网站的示例代码,我已经设置好了。当它运行时,有东西被上传到服务器(或至少它离开我的计算机)。我可以监视上传,当上传完成时,服务器识别它完成并加载文件,但它加载一个旧的XML。所以它正确连接到服务器,但我不知道为什么文件上传不能正常工作。我只是想发送文件,服务器不需要任何头或参数等。

所以我想知道我是否正确存储的数据?或者如果我不发送它的服务器正确或什么?任何建议都会有帮助

So I'm trying to upload an XML file to a server with POST using AFNetworking.
So using example code from their site I have this set up. When it runs, something is uploaded to the server (or at least it leaves my computer). I can monitor the upload, when the upload is finished, the server recognizes that it completed and goes to load the file, but it loads an old XML. So its connecting properly to the server, but I'm not sure why the file upload isn’t working correctly. Also I just want to send the file, the server doesn’t need any headers or parameters etc.
So I'm wondering if I’ve stored the data correctly? Or if I'm not sending it the server properly or what? Any suggestions would be helpful

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


推荐答案

操作?在 setUploadProgressBlock 之后尝试此操作:

Have you tried to catch the success/failure of the operation? Try this after 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似乎确定。

This is an easy way to know what your server returned. If something uploads to your server, double check that you're getting the right file. AFAIK, your AFNetwork seems ok.

这篇关于使用AFNetworking通过POST上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆