NSURLConnection异步上传文件? [英] NSURLConnection to upload file asynchonrously?

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

问题描述

我是ios开发的新手, 我一直在阅读和搜索,但找不到有效的示例或 如何将文件从iPhone异步上传到Web服务器.

I'm kinda of new at ios development, I've been reading and searching but cannot find a working example or an example of how to upload a file from iphone to webserver asychronously..

我可以使用

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

它可以工作,但是会阻塞我的主线程.

it works, but it blocks my main thread.

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

NSUrlConnection has this delegate (connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:)

但是我不知道如何实现它.

but I've no idea how to implement it.

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

Can someone please point me in the right direction?

推荐答案

我设法通过以下方法与NSURLConnection异步上传:

I have managed to get uploading to work with NSURLConnection asynchronously with this:

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

这篇关于NSURLConnection异步上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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