2010-12-19 198 views
2

我正在尝试从iPhone应用程序上传图像到服务器从iPhone应用程序上传图片

上传图片的PHP代码如下

if(isset($_POST['insertImage'])) 
{     //INSERT IMAGE ------------------- 
    $method=safeData($_POST['insertImage']); 
    if($method=='true') 
    { 
     if(isset($_POST['userId'])) 
     { 

      if(isset($_FILES['imageFile'])) 
      { 

      if($_FILES['imageFile']['type']=="image/gif" || $_FILES['imageFile']['type']=="image/bmp" || $_FILES['imageFile']['type']=='image/jpeg' || $_FILES['imageFile']['type']=='image/jpg' || $_FILES['imageFile']['type']=='image/png') 
      { 
       if($_FILES['imageFile']['size']<=5250000) 
       { 
                       $userId=safeData($_POST['userId']); 
        $newImgName=rand()."a".time().".".findexts($_FILES["imageFile"]["name"]);      imgPath="./../admin/images/"; 
        move_uploaded_file($_FILES['imageFile']['tmp_name'],$imgPath.$newImgName); 
        $data.=saveImageInfo($userId,$newImgName); 
       } 
        else 
       { 
        $data.="<List><ResponseCode>405</ResponseCode><Message>Maximum image size should not be more than 5mb </List>"; 
       } 
      } 
      else 
      { 
       $data.="<List><ResponseCode>405</ResponseCode><Message>Invalid image format. only png,jpg,bmp formats supported</Message></List>";              } 
      } 
      else 
      { 
       $data.="<List><ResponseCode>405</ResponseCode><Message>imageFile method not found</Message></List>";    
      } 
                    } 
     else 
     { 
      $data.="<List><ResponseCode>405</ResponseCode><Message>userId method not found</Message></List>"; 
     } 
    } 
    else 
    { 
     $data.="<List><ResponseCode>405</ResponseCode><Message>invalid insertImage argument</Message></List>";    
    } 
} 

我使用以下代码将图片上传到服务器

+(NSData *)setUserImage:(NSData *)userImageData UserId:(int)UserId 
{ 
    NSString *result; 
    NSData *responseData; 
    @try { 
     NSURL *url = [[NSURL alloc] initWithString:webAddress]; 
     NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url]; 
     [req setHTTPMethod:@"POST"]; 

     [req setValue:@"multipart/form-data; boundary=*****" forHTTPHeaderField:@"Content-Type"];// 

     NSMutableData *postBody = [NSMutableData data]; 
     NSString *stringBoundary = [NSString stringWithString:@"*****"]; 

     [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]]; 
     [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"insertImage\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]]; 
     [postBody appendData:[[NSString stringWithFormat:@"true"] dataUsingEncoding:NSASCIIStringEncoding]]; 
     [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]]; 


     [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]]; 
     [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userId\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]]; 
     [postBody appendData:[[NSString stringWithFormat:@"%d",UserId] dataUsingEncoding:NSASCIIStringEncoding]]; 
     [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]]; 

     [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]]; 
     [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"imageFile\"; filename=\"myimagefile.png\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]]; 

     //[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"imageFile\"; filename=\"myimagefile.png\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]]; 
     [postBody appendData:[NSData dataWithData:userImageData]];// dataUsingEncoding:NSASCIIStringEncoding]]; 
     [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]]; 

     [req setHTTPBody: postBody];//putParams]; 

     NSHTTPURLResponse* response = nil; 
     NSError* error = [[[NSError alloc] init] autorelease]; 

     responseData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error]; 
     result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
     if(isInDebugMode) 
      NSLog(@"Result: %@", result); 

     [url release]; 
     [req release]; 

     IceCreamManFinderAppDelegate *delegate1=(IceCreamManFinderAppDelegate *)[UIApplication sharedApplication].delegate; 
     if(error.domain!=nil) 
     { 
      NSString *errorDesc=[[error userInfo] objectForKey:@"NSLocalizedDescription"]; 
      delegate1.globalErrorMessage=errorDesc; 
      return nil; 
     } 
     else 
     { 
      delegate1.globalErrorMessage=nil; 
     } 
    } 
    @catch (NSException* ex) { 
     NSLog(@"Error: %@",ex); 
    } 
    return responseData; 
} 

from above代码我从服务器获得以下响应

图像格式无效。只有png,jpg,bmp格式

我尝试了很多但没有成功。

请建议我哪里错了?

回答

3

您编写的服务器代码只接受某些指定内容类型的数据,但是您从未将内容类型添加到数据中。简单地发出正确的内容类型的图像数据:

// Emit the content type here as well 
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]]; 
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"imageFile\"; filename=\"myimagefile.png\"\r\nContent-Type: image/png\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]]; 

[postBody appendData:[NSData dataWithData:userImageData]]; 
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]]; 

错误实际上是未来从您自己的服务器响应回来,所以只需控制对服务器代码的流程下面给你的原因。

+0

嗨杰森,我知道这件事情,错误是从服务器回来。最主要的是图像上传不起作用,因为我犯了一些愚蠢的错误。非常感谢 。现在它的工作 – user547503 2010-12-19 11:31:24

+0

嗨贾森,我有同样的问题,因为在问题。我没有得到你想说的你的代码..请通过更改后发表相同的代码..谢谢 – dks1725 2011-02-04 09:43:38