NSURLConnection上传文件异常?

时间:2011-09-01 05:39:30

标签: objective-c

我在ios开发方面有点新鲜, 我一直在阅读和搜索,但找不到一个工作的例子或一个例子 如何异步地将文件从iphone上传到网络服务器..

我可以使用

同步上传
[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

它有效,但它会阻止我的主线程。

NSUrlConnection具有此委托(connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:)

但我不知道如何实现它。

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

3 个答案:

答案 0 :(得分: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];

答案 1 :(得分: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框架,您可以在此处找到更多信息:{{3} }

要使用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];

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

答案 2 :(得分:2)

您无法为该任务创建新线程:

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

当您完成上传时,只需在主线程中调用一些已实现的方法(例如uploaded):

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