我们如何在iPhone上进行后台文件上传?

时间:2009-09-11 02:55:24

标签: iphone file upload background

像qik.com,ustream.com,我希望知道如何通过iPhone SDK 3.0在后台上传文件。请告诉我。谢谢 !

5 个答案:

答案 0 :(得分:2)

目前还不清楚“在后台”是什么意思,但如果你只是想要异步上传,可以使用NSURLConnection,NSURLRequest,或者你可以使用这个名为ASIHTTPRequest的优秀库。它工作得很好,并提供了一种显示下载和上传进度的简单方法。

答案 1 :(得分:0)

你可以为你的文件上传开始一个新的线程,查看NSThread类继承人链接http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/Reference/Reference.html ...说你也可以使用NSURLConnection中的asynchronousRequest,它为你启动一个线程,这是一个引用{{ 3}}

答案 2 :(得分:0)

你可以这样做(这基本上是从我的一个项目中剪切和粘贴)。积分在发展论坛上发布了一些帖子,但我不知道它是谁:

- (IBAction)startUpload:(id)sender {
    NSString *filename = [NSString stringWithFormat:@"iphone-%d.png", [NSDate timeIntervalSinceReferenceDate]];

    NSString *boundary = @"----BOUNDARY_IS_I";

    NSURL *url = [NSURL URLWithString:@"http://yourgreatwebsite.com/upload"];

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

    [req setHTTPMethod:@"POST"];

    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];

    [req setValue:contentType forHTTPHeaderField:@"Content-type"];

    NSData *imageData = UIImagePNGRepresentation([imageView image]);

    // adding the body
    NSMutableData *postBody = [NSMutableData data];


    // first parameter an image
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filename\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:imageData];

    // second parameter information
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    //[postBody appendData:[@"Content-Disposition: form-data; name=\"some_other_name\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    //[postBody appendData:[@"some_other_value" dataUsingEncoding:NSUTF8StringEncoding]];
    //[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r \n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [req setHTTPBody:postBody];

    //start the spinner and deactivate the buttons...
    [self setButtonsEnabled:NO];


    [[NSURLConnection alloc] initWithRequest:req delegate:self];
  }

  #pragma mark urlconnection delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // this method is called when the server has determined that it
    // has enough information to create the NSURLResponse

    // it can be called multiple times, for example in the case of a
    // redirect, so each time we reset the data.
    // receivedData is declared as a method instance elsewhere
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // append the new data to the receivedData
    // receivedData is declared as a method instance elsewhere
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    // release the connection, and the data object
    [connection release];

    // inform the user
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Upload Error" message:[NSString stringWithFormat:@"The upload failed with this error: %@", [error localizedDescription]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];

    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    // receivedData is declared as a method instance elsewhere
#ifdef DEBUG
    NSLog(@"upload succeeded!");
#endif

    // release the connection, and the data object
    [connection release];

    NSString *response = [NSString stringWithCString:[receivedData bytes] length:[receivedData length]];    


#ifdef DEBUG
    NSLog(response);
#endif
}

答案 3 :(得分:0)

如果您在应用未运行时引用后台上传,则不能(操作系统不允许)。如果它是应用程序运行时的背景,那么此处发布的链接和示例代码可以正常工作。

答案 4 :(得分:0)

请参阅this post了解一个(经过深思熟虑的)回复。

虽然当您的应用未运行时无法在后台上传文件,但是当您的应用运行时,完全可以这样做。这样你就不会影响前台线程,而且你可以增加它以显示进度等。