-1

我尝试使用此代码将两个图像上传到 PHP 服务器。但响应是“错误”。我想知道,这种方法适用于 iOS 5 吗?或者我的代码有什么问题?

- (IBAction)uploadImage
{    
    NSString *filename= @"After";
    NSString *filename2= @"Before";

    // grab images from documents directory
    NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
    NSString *docDirectory = [sysPaths objectAtIndex:0];

    NSString *filePath = [NSString stringWithFormat:@"%@/%@",docDirectory,@"After.png"];
    NSData *imageData = [[NSData alloc]initWithContentsOfFile:filePath];
    NSString *filePath2 = [NSString stringWithFormat:@"%@/%@",docDirectory,@"Before.png"];
    NSData *imageData2 = [[NSData alloc]initWithContentsOfFile:filePath2];

    // setting up the URL to post to
    NSString *urlString = @"http://uploadimage.php";

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

    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    // body of the post
    NSMutableData *body = [NSMutableData data];

    [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@.png\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];

    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image2\"; filename=\"%@.png\"\r\n", filename2] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData2]];

    [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

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

    //NSDictionary *response = [[NSDictionary alloc] init];
    //response = [returnString JSONValue];
    NSLog(@"Return string : %@", returnString);
}

如果这段代码没问题,那么我可以理解错误在 PHP 端。

4

1 回答 1

0

使用ASIHTTPRequest

  -(void)uploadImage
   {
    NSData *imageData = UIImageJPEGRepresentation(imageobj, 90);
    NSURL *url = [NSURL URLWithString:@"url"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setDelegate:self];
    [request setPostValue:@"myImageName" forKey:@"name"];
    [request setData:imageData forKey:@"file"];

    [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *responseString = [request responseString];
    NSLog(@\"response: %@\", responseString);

    // Use when fetching binary data
    NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
}
于 2012-09-05T09:31:59.740 回答