1

ブロブを他の値と一緒に mysql データベースに挿入しようとしていますが、他の値はうまくいき、エラーはありません。最大許容パケット サイズは 10 MB に設定され、写真データは 0.5 MB です。

写真をバイナリに変換するコードは次のとおりです。

- (NSData *)dataFromImage:(UIImage *)image{
    NSData *imageData = UIImagePNGRepresentation(image);
    return imageData;
}

次に、php スクリプトにポストされます。

$db = new mysqli(localhost, $database, $pass, $table);

if ($query = $db->prepare("INSERT INTO JobData (someotherdata, photo) values (? ,?)")) {

    $query->bind_param("sb", 
            $_POST["someotherdata"],
            $_POST["photo"]);

    $query->execute();

他のデータはデータベースに表示されますが、写真フィールドには 0 バイトが表示されます。mysqli_error はエラーを生成しません。

NSData ログ:

4f1eba47 4f37e5f1 d3475bf6 d9fef291 7b1cc893 af9e38c9 d3674fdd 13e4e9b3...etc

多分私はデータをバイナリに正しく変換していませんか? 私はウェブ上でこれについて多くを見つけることができません.私が見つけたもののほとんどはsqliteに関係しているので、どんな助けも大歓迎です.

また、bind_param 呼び出しの前にすべてのデータが設定されているかどうかも確認します。

4

1 に答える 1

3

私は最近、画像を伴うSQLデータベースに大量の情報(名前、ID、年齢など)をアップロードする必要がある同様のプロジェクトを行っています。残念ながら、データをバイナリに変換して送信するほど簡単ではありませんが、それを行うための素晴らしいコードがあります..

//this is where you put in the data e.g...
 NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];
[_params setObject:userName forKey:@"sendername"];
//[_params setObject:[NSString stringWithFormat:@"%d", userId] forKey:[NSString stringWithString:@"userId"]];
//[_params setObject:[NSString stringWithFormat:@"%@",_title] forKey:@"title"];

// the boundary string : a random string, that will not repeat in post data, to separate post data fields.
NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy";

// string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ
NSString* FileParamConstant = @"picture";

// the server url to which the image (or the media) is uploaded. Use your server url here
NSURL* requestURL = [NSURL URLWithString:@"url name here"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];

// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

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

// add params (all params are strings)
for (NSString *param in _params) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}

// add image data
UIImage *imageToPost = [UIImage imageNamed:@"image is here"];

NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
if (imageData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

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

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

// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

// set URL
[request setURL:requestURL];

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];



[connection start];

それを試してみて、あなたがどのようにうまくいくか教えてください.それはあなたが探しているものとはまったく異なるかもしれませんが、うまくいけば、正しい方向にあなたを導きます. T

于 2013-03-04T04:23:35.677 に答える