gpt4 book ai didi

php - 使用 xcode 和 php 将图像上传到网络服务器

转载 作者:行者123 更新时间:2023-12-02 07:32:54 27 4
gpt4 key购买 nike

我在获取一些已获得的代码时遇到了一些问题,我在 php/html 方面的经验充其量是“有限的”,所以我希望我问这个问题不会看起来像个菜鸟。

我遇到的问题是无法将小图像上传到网络服务器。在过去的 4 到 5 个小时里,我一直在研究以下内容,现在英国已将近凌晨 2 点,我取得的唯一进展是不断变化

[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

[NSString stringWithFormat:@"enctype=\"multipart/form-data\"; boundary=%@",boundary];

这让我的服务器在每次发送内容时停止给出内部错误。

代码在下面,它在这个网站的许多页面上,我找不到原作者来表示信任。当我在接收 php 文件中有 print_r($_FILES); 时,我目前得到一个空数组。

任何帮助都将不胜感激,在此先感谢。

NSString *urlString = @"http://mysite/upload_image.php";

UIImage * img = [UIImage imageNamed:@"mypic.png"];
NSData *imageData = UIImageJPEGRepresentation(img, 90);

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

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


NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: attachment; name=\"image\"; filename=\"davesfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

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

NSLog(@"%@",returnString);

最佳答案

几个 react :

  1. 如果您使用 AFNetworking ,创建这类请求的过程更简单,让您摆脱自己构建请求的杂草:

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:[url absoluteString] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData name:fieldName fileName:[imagePath lastPathComponent] mimeType:[self mimeTypeForPath:imagePath]];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"Success: %@", string);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
    }];
  2. 顺便说一下,我通常使用 MobileCoreServices.framework 来根据扩展名获取 mime 类型,而不是对其进行硬编码:

    - (NSString *)mimeTypeForPath:(NSString *)path
    {
    // get a mime type for an extension using MobileCoreServices.framework

    CFStringRef extension = (__bridge CFStringRef)[path pathExtension];
    CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, extension, NULL);
    assert(UTI != NULL);

    NSString *mimetype = CFBridgingRelease(UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType));
    assert(mimetype != NULL);

    CFRelease(UTI);

    return mimetype;
    }
  3. 我注意到您正在将图像检索到 UIImage 中,然后使用 UIImageJPEGRepresentation 获取 NSData。如果你想或必须这样做,但我通常更喜欢发送原始数据,直接通过 UIImage 绕过往返,这会导致图像质量下降和元数据丢失。相反,您可能想直接获取 NSData:

    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"mypic" ofType:@"png"]; // note, I'm going back to bundle, not grabbing `UIImage` from imageview
    NSData *imageData = [NSData dataWithContentsOfFile:imagePath];

    如果您真的必须通过 UIImage 进行往返,那么您可能希望使用 imageWithContentsOfFile 而不是 imageNamed,因为后者会缓存不必要的图像。

  4. 如果您决定自己创建请求,这是我过去使用的例程:

    - (void)uploadFileAtPath:(NSString *)imagePath
    forField:(NSString *)fieldName
    URL:(NSURL*)url
    parameters:(NSDictionary *)parameters
    {
    NSString *filename = [imagePath lastPathComponent];
    NSData *imageData = [NSData dataWithContentsOfFile:imagePath];

    NSMutableData *httpBody = [NSMutableData data];
    NSString *boundary = [self generateBoundaryString];
    NSString *mimetype = [self mimeTypeForPath:imagePath];

    // configure the request

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

    // set content type

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

    // add params (all params are strings)

    [parameters enumerateKeysAndObjectsUsingBlock:^(NSString *parameterKey, NSString *parameterValue, BOOL *stop) {
    [httpBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [httpBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", parameterKey] dataUsingEncoding:NSUTF8StringEncoding]];
    [httpBody appendData:[[NSString stringWithFormat:@"%@\r\n", parameterValue] dataUsingEncoding:NSUTF8StringEncoding]];
    }];

    // add image data

    if (imageData) {
    [httpBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [httpBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", fieldName, filename] dataUsingEncoding:NSUTF8StringEncoding]];
    [httpBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", mimetype] dataUsingEncoding:NSUTF8StringEncoding]];
    [httpBody appendData:imageData];
    [httpBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    }

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

    // setting the body of the post to the reqeust

    [request setHTTPBody:httpBody];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    if (connectionError)
    {
    NSLog(@"sendAsynchronousRequest error=%@", connectionError);
    return;
    }

    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"Success: %@", string);
    }];
    }

    显然,如果您没有使用任何参数(您似乎没有这样做),您可以只为该参数传递nil。但关键是它支持额外的参数,如果你需要的话。我还使用异步网络请求,因为永远不应该在主队列上发出同步网络请求。

    这使用了 Apple 的 SimpleURLConnections 示例中的 generateBoundaryString 方法:

    - (NSString *)generateBoundaryString
    {
    // generate boundary string
    //
    // adapted from http://developer.apple.com/library/ios/#samplecode/SimpleURLConnections
    //
    // Note in iOS 6 and later, you can just:
    //
    // return [NSString stringWithFormat:@"Boundary-%@", [[NSUUID UUID] UUIDString]];

    CFUUIDRef uuid;
    NSString *uuidStr;

    uuid = CFUUIDCreate(NULL);
    assert(uuid != NULL);

    uuidStr = CFBridgingRelease(CFUUIDCreateString(NULL, uuid));
    assert(uuidStr != NULL);

    CFRelease(uuid);

    return [NSString stringWithFormat:@"Boundary-%@", uuidStr];
    }
  5. 如果您愿意,我仿照上面的一些代码改进了您的代码:

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    [request setHTTPMethod:@"POST"];

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

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", fieldName, [imagePath lastPathComponent]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", [self mimeTypeForPath:imagePath]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];

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

    NSLog(@"%@",returnString);
  6. 就我个人而言,我建议更改您的 PHP 代码以生成 JSON 响应(因为这将更容易让您的应用使用)。

顺便说一下,我已经用我的 PHP 代码测试了上面的内容。如果您仍有问题,我建议您也与我们分享您的 PHP 代码,因为格式良好的请求的构造与 PHP 代码的要求密切相关。

关于php - 使用 xcode 和 php 将图像上传到网络服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20026206/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com