2011-03-29 47 views
5

有没有办法将audiofile异步上传到我的服务器?我已经使用NSURLConnection类同步实现了它。使用iOS上传异步音频文件

也许你也可以给我一个简短的代码示例。

感谢,

回答

8

你绝对必须使用ASIHttpRequest

它非常容易 - 它是所有iOS上最流行的库。

只需选择“异步”模式。

它几乎不可能很容易使用。基本上只需输入网址。

http://allseeing-i.com/ASIHTTPRequest/How-to-use

享受!


这是惊人的消息,本不再ASIHttpRequest!

你可以阅读所有关于它:

http://allseeing-i.com/[request_release]。

+0

感谢您的回答。我设置正确,我确实从服务器得到了回应。我想发送'NSData',但'[request addPostValue:file forKey:@“file”];'不适合我。有什么建议么? – hgbnerd 2011-03-30 07:39:31

+1

谢谢,'setData'为我做了。不幸的是,我不能满意,因为我需要15个代表点); – hgbnerd 2011-03-30 14:37:26

+1

我读到ASIHTTPRequest在iOS 5上不再支持 – antf 2012-08-12 23:17:29

0

现在是2015年。iOS 9.1刚刚发布。 AFNetworking已成为处理HTTP请求最流行的库。 It supports asynchronous file uploads。这里有一个例子:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 

NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 

NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"]; 
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { 
    if (error) { 
     NSLog(@"Error: %@", error); 
    } else { 
     NSLog(@"Success: %@ %@", response, responseObject); 
    } 
}]; 
[uploadTask resume];