1

http-post リクエストを使用して、保存した写真 (iPhone 4) から画像をアップロードしようとしています。$_FILES を適切な場所に移動するのに問題なく動作する uploader.php を取得しました。しかし、最後に、画像をアップロードすると、次のように表示されます。

POSTが成功した後、ftpからダウンロードした画像

ここで問題を解決し、関連する質問を探すのに何時間も費やしましたが、何もうまくいきません。

-(void)sendImage:(UIImage*)image{
    NSString *url = @"http://www.mypage.vv.si/uploader.php";
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
                                                            cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                        timeoutInterval:60.0f];
    [theRequest setHTTPMethod:@"POST"];

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

    NSData *imageData = UIImageJPEGRepresentation(image, 1);
    if (imageData)
    {
        NSMutableData *body = [NSMutableData data];

        [body appendData:[[NSString stringWithFormat:@"Content-Length %d\r\n\r\n", [imageData length] ] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        NSString *timeStr = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]];
        timeStr = [timeStr stringByReplacingOccurrencesOfString:@"." withString:@""];
        NSString *uniqueFileName = [NSString stringWithFormat:@"img%@.jpg", timeStr];
        NSString *contDispos = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", uniqueFileName];

        [body appendData:[contDispos dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];    
        [body appendData:imageData];

        [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [theRequest setHTTPBody:body];


        NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
        [theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
        NSURLConnection *connection = [NSURLConnection connectionWithRequest:theRequest delegate:self];
        if (connection) {
            self.infoData = [NSMutableData data];
        } else {
            NSLog(@"Connection failed");
        }
    }
    else
        NSLog(@"No Data!");
}
4

0 に答える 0