How can we do Background File Upload in iphone?

廉价感情. 提交于 2019-12-09 07:13:31

问题


like qik.com , ustream.com , I hope to know how to upload file in background via iPhone SDK 3.0 . Pls let me know . Thanks !


回答1:


It's not clear what you mean by "in background", but if you just mean you want to upload asynchronously, you can use NSURLConnection, NSURLRequest, or you can use this excellent library called ASIHTTPRequest. It works great and provides a simple way to show download and upload progress.




回答2:


You can start a new thread for your file upload, look into the NSThread class heres a link http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/Reference/Reference.html ...that said you can also use asynchronousRequest from NSURLConnection which starts a thread for you heres a reference http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html




回答3:


You could do it this way (this is basically cut & paste from one of my projects). Credit goes to some post on the development forums, but I don't know who it was from anymore:

- (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
}



回答4:


If you're refering to background upload while app is not running, you can't (OS doesn't allow it). If it's background while app is running, links and sample code posted here work just fine.




回答5:


See this post for one (very well thought-out) response.

While it's not possible to upload files in the background when your app is NOT running, it's entirely possible to do so when your app IS running. This way you don't affect your foreground thread, plus you can likely augment this to show progress, etc.



来源:https://stackoverflow.com/questions/1408759/how-can-we-do-background-file-upload-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!