2011-09-01 46 views
2

我有点新的iOS开发, 我一直在阅读和搜索,但无法找到工作的例子或 如何上传从iphone文件到web服务器异步地为例..NSURLConnection上传文件asynchonrously?

我能够同步上传

[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

它的工作原理,但它阻止我的主线程。

NSURLConnection的有这种委托(connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:)

,但我不知道如何实现它。

有人可以请指出我在正确的方向吗?

+0

我建议你创建新的线程,你将执行'[NSURLConnection的sendSynchronousRequest:request returningResponse:nil error:nil];'。 – Nekto

+0

我已经考虑为每次上传创建一个线程,但是如何获得关于上传成功或失败的反馈? – skeo

+0

请看我的回答,请 – Nekto

回答

2

你不能为任务创建新的线程:

[NSThread detachNewThreadSelector:@selector(upload:) toTarget:self withObject:data]; 

而当你将完成上传,只需调用一些实现方法在你的主线程(例如uploaded):

[self performSelectorOnMainThread:@selector(uploaded) withObject:nil waitUntilDone:NO]; 
5

线程对于这类问题不是必需的。我会建议你使用内置到NSURLConnection的异步功能:

NSURLConnection *connection = [NSURLConnection initWithRequest:request delegate:self]; 
[connection start]; 


- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    //Get your response, do stuff 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    //Show error, retry, etc 
} 

编辑:如果你真的希望上传文件并希望显示一个进度条,那么你应该看看ASIHTTPRequest框架,您在这里可以找到更多的信息:http://allseeing-i.com/ASIHTTPRequest/

对于上载使用ASIHTTPRequest数据POST请求,你会怎么做:

NSURL *url = [NSURL URLWithString:@"YOUR_URL_HERE"]; 
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
[request setPostValue:@"123" forKey:@"user_id"]; 
[request setData:someData withFileName:@"someData.jpg" andContentType:@"image/png" forKey:@"image"]; 
[request setProgressDelegate:self]; 
[request setDelegate:self]; 
[request startAsynchronous]; 

再次,请参阅上面的链接为框架和文档。我强烈推荐它。

+0

是的,这是更好的决定。 – Nekto

+0

尼克,请您详细说明一下NSURLConnection的说法:通过这种方式发送NSData或jpeg文件?我可以使用这种方法异步下载,但超出了我如何设置包装文件上传。如果一切都失败了,我会看看ASIHTTPResquest – skeo

+1

我会在NSData中包含NSURLConnection的示例代码,但是我没有这样做,因为我总是需要multipart-formdata,这与NSURLConnection是一场噩梦。 –

13

我设法让上传与NSURLConnection的异步这项工作:

-(NSURLRequest *)postRequestWithURL: (NSString *)url 

           data: (NSData *)data 
          fileName: (NSString*)fileName 
{ 

// from http://www.cocoadev.com/index.pl?HTTPFileUpload 

    //NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; 

    NSMutableURLRequest *urlRequest = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [urlRequest setURL:[NSURL URLWithString:url]]; 
    //[urlRequest setURL:url]; 

    [urlRequest setHTTPMethod:@"POST"]; 

    NSString *myboundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",myboundary]; 
    [urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"]; 


    //[urlRequest addValue: [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundry] forHTTPHeaderField:@"Content-Type"]; 

    NSMutableData *postData = [NSMutableData data]; //[NSMutableData dataWithCapacity:[data length] + 512]; 
    [postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", myboundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", fileName]dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postData appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postData appendData:[NSData dataWithData:data]]; 
    [postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", myboundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    [urlRequest setHTTPBody:postData]; 
    return urlRequest; 
} 

用法如下:

NSURLRequest *urlRequest = [self postRequestWithURL:urlString 
                data:aVideo.msgvid 
               fileName:filename]; 

    uploadConnection =[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; 
+2

这看起来类似于我正在拾取的代码,实际上是多部分边界匹配。伙计,'兰特()'。 – Potatoswatter