【问题标题】:ASIHTTPRequest Image Upload Not Working For Certain ImagesASIHTTPRequest 图像上传不适用于某些图像
【发布时间】:2013-01-11 03:37:58
【问题描述】:

当我尝试将图像上传到 php 脚本时,我在使用 ASIHTTPRequest 库时遇到了一个奇怪的问题。我已经正确实现了 ASIHTTPRequest,因为 php 脚本确实从 iphone 模拟器接收 POST 数据,但仅适用于我的测试集中的一些图像。还有其他图片没有通过 POST。

所有图片均取自 Facebook,格式为 jpg 或 png。我已经在这两种类型的图像上测试了我的代码,但这并不重要,因为我在 iphone 应用程序中使用 PNGRepresentation 方法将图像转换为 NSData。我还测试了图像的大小,这不是问题(范围从 600x600 到 1200x1200)。

破坏 ASIHTTPRequest 的图像对我来说似乎一点也不特别,而且我在识别错误​​时遇到了麻烦。下面是我的一些实现:

iPhone 实施:

[RegisterRequest setData:profilePicturePNG withFileName:filename andContentType:@"image/png" forKey:@"profilePicture"];
[RegisterRequest addRequestHeader:@"Content-Type" value:@"image/png"];
[RegisterRequest setDelegate:self];
[RegisterRequest startAsynchronous];

PHP 实现:

echo "Upload: " . $_FILES["profilePicture"]["name"] . "<br>";
echo "Type: " . $_FILES["profilePicture"]["type"] . "<br>";
echo "Size: " . ($_FILES["profilePicture"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["profilePicture"]["tmp_name"] . "<br>";

在这个测试中,PHP 实现应该回显文件属性。正如我之前所说,对于大多数图像,我确实得到了回声。但是有些图片的名称和类型不经过,大小报告为 0kb。

有什么建议吗?我将不胜感激!

【问题讨论】:

    标签: iphone xcode image asihttprequest


    【解决方案1】:

    为了您的信息,ASIHTTPRequest 已被弃用,但您可以在此处使用 AFNetworking,您可以从中参考。 https://github.com/AFNetworking/AFNetworking 还有这个例子

    -(IBAction)uploadButtonClicked:(id)sender{
    
    NSData *imageToUpload = UIImageJPEGRepresentation(mainImageView.image, 90);
    
    AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.THESERVER.com"]];
    
    NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/PROJECT/upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData: imageToUpload name:@"file" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
    }];
    
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *response = [operation responseString];
    NSLog(@"response: [%@]",response);
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    if([operation.response statusCode] == 403){
        NSLog(@"Upload Failed");
        return;
    }
    NSLog(@"error: %@", [operation error]);
    
    }];
    
    [operation start];
    }
    

    【讨论】:

      【解决方案2】:

      我猜你不应该使用 ASIHTTPRequest 来上传图片

       NSString *requestStr = [yourPHPSCriptURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
      responseString = nil;
      
      NSURL *requestUrl = [[NSURL alloc] initWithString:requestStr];
      
      ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:requestUrl];
      
      [request setPostFormat:ASIMultipartFormDataPostFormat];
      [request addPostValue:@".png" forKey:image_content_type];  //Use this if you want//
      [request setShouldAttemptPersistentConnection:YES];
      [request setData:yourPhotoData withFileName:@"user_image_byte.png" andContentType:@"image/png" forKey:@"user_image_byte"];
      
      [request startSynchronous];
      

      我将图像作为字节流发送,然后在 asp.net 中将其转换回图像。希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-28
        • 2022-01-26
        • 2015-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多