0

我正在向服务器发送文件。就像在本教程中一样:http: //zcentric.com/2008/08/29/post-a-uiimage-to-the-web/#comment-8145

- (IBAction)uploadImage {
/*
 turning the image into a NSData object
 getting the image back out of the UIImageView
 setting the quality to 90
*/
NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
// setting up the URL to post to
NSString *urlString = @"http://iphone.zcentric.com/test-upload.php";

// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

/*
 add some header info now
 we always need a boundary when we post a file
 also we need to set the content type

 You might want to generate a random boundary.. this is just the same 
 as my output from wireshark on a valid html post
*/
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

/*
 now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="userfile"; filename="ipodfile.jpg"rn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];

// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(returnString);
}

从服务器端:

$uploaddir = './uploads/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "http://iphone.zcentric.com/uploads/{$file}";
}

任何人都可以请回答我:

  • 检查文件是否已完全上传或是否存在错误并将过程结果从服务器发送到应用程序的任何方式。
  • 如果出现中断(例如按下主页按钮或打电话或网络下降),应用程序将在应用程序再次处于活动状态时完成上传。

基本上我必须在文件上传时更新数据库,但我必须确保没有错误。

4

1 回答 1

0

只要您的服务器以上传文件的 URL 响应(这意味着它已成功上传),您就可以检查returnString是否为非空且包含有意义的 URL 以及返回的代码和错误。

NSHTTPURLResponse* response;
NSError* err;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
if (err) {
    //Network error happened (DOESN'T mean HTTP error code)
    NSLog(@"Error: %@", err);
    return;
}
if ([response statusCode] != 200) { 
    //Server returned HTTP error code
    NSLog(@"Error: Server returned non 200 code");
    return;
}
if (![NSURL urlWithString:returnString]) { 
    //URL is not valid
    NSLog(@"File URL returned from server is not valid");
    return;
}
//File uploaded
NSLog(@"File uploaded successfully");

此示例假设您在项目中使用 ARC(因此没有发布)。

要正确处理应用程序挂起,请查看后台任务。您可以从文件上传代码制作后台任务,如果应用程序移至后台,它应该继续上传。

于 2012-09-06T18:57:17.047 回答