1

我正在尝试将 blob 与其他值一起插入到 mysql 数据库中,其他值正常并且没有任何错误。最大允许数据包大小设置为 10mb,照片数据为 0.5mb。

这是将照片转换为二进制的代码:

- (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();

otherdata 显示在数据库中,但 0 字节显示在 photo 字段中。mysqli_error 不会产生任何错误。

NSData 日志:

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

也许我没有正确地将数据转换为二进制?我在网上找不到太多关于此的信息,我发现的大部分内容都与 sqlite 有关,因此非常感谢任何帮助。

我还检查是否在调用 bind_param 之前设置了所有数据

4

1 回答 1

3

我最近一直在做一个类似的项目,我需要将一堆信息(姓名、id、年龄等)上传到一个带有图像的 sql 数据库中。不幸的是,它并不像将数据转换为二进制并发送它那么容易,但是有一段很棒的代码可以为你做到这一点..

//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];

试一试,让我知道你的进展情况......它可能不是你想要的,但希望它能引导你朝着正确的方向前进。吨

于 2013-03-04T04:23:35.677 回答