2013-12-11 33 views
1

我正在使用类似iPhone的应用程序。根据我的要求,我需要发送相机捕获的图像到服务器使用PHP网址。我将捕获的图像存储在应用程序资源文档目录中。拍摄照片并将其发送到iPhone中的PHP服务器

这里是我的PHP代码 -

$file_path = "../Gallery/"; 

    $file_path = $file_path . basename($_FILES['Documents']['name']); 
    if(move_uploaded_file($_FILES['Documents']['tmp_name'], $file_path)) { 
     echo "Image is upload"; 
    } else{ 
     echo "Image is not Upload"; 
    } 

这里是我的iPhone的代码 -

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    CGFloat compression = 0.5f; 

    UIImage *smallSizeImage = [self scaleImage:image toSize:CGSizeMake(140.0, 80.0)]; 
    imageData = UIImageJPEGRepresentation(smallSizeImage, compression); 

    ivPicture.image = smallSizeImage; 

    self.passingNbPosition.positionImageId = [Util getNewGUID]; 

    [imageData writeToFile:[Util getFilePathForFileName:[NSString stringWithFormat:@"%@.jpg",self.passingNbPosition.positionImageId]] atomically:YES]; 


    // setting up the URL to post to 
    NSString *urlString = @"http://...........php"; 

    // setting up the request object now 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setHTTPMethod:@"POST"]; 

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

    /* 
    now lets create the body of the post 
    */ 
    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", self.passingNbPosition.positionImageId] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"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]]; 
    // setting the body of the post to the reqeust 
    [request setHTTPBody:body]; 

    // now lets make the connection to the web 
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

    NSLog(returnString); 

} 

使用上面的代码在控制台上打印时,我得到的回报数据,但返回的字符串来了之后,“图片不上传“。我不知道我的错误在哪里。我认为这是我的iPhone代码中的一个问题。有人可以帮助我,我在这里做错了。

谢谢..

+0

你听说过'AFNetworking'?试试这个第三方库打网址'https:// github.com/AFNetworking/AFNetworking' – Mani

+1

谢谢,但我想知道我的上述代码中的错误在哪里? –

+0

@KumarKlYes队友..我越来越。 –

回答

1
- (void) saveImageInServer 
{ 
    /* 
    turning the image into a NSData object 
    getting the image back out of the UIImageView 
    setting the quality to 90 
    */ 
     NSData *imageData = UIImageJPEGRepresentation(self.profilePicture.image, 90); 
     // setting up the URL to post to 
     NSString *urlString = @"http://webservices/image-upload.php"; 

     // setting up the request object now 
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ; 
     [request setURL:[NSURL URLWithString:urlString]]; 
     [request setHTTPMethod:@"POST"]; 

     /* 
     add some header info now 
     we always need a boundary when we post a file 
     also we need to set the content type 

     You might want to generate a random boundary.. this is just the same 
     as my output from wireshark on a valid html post 
     */ 
     NSString *boundary = @"---------------------------147378098314876653456641449"; 
     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
     [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

     /* 
     now lets create the body of the post 
     */ 
     NSMutableData *body = [NSMutableData data]; 
     [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     NSString *stringData = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\%@.jpg\r\n",self.emailTextField.text]; 
     [body appendData: [stringData 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]]; 
     // setting the body of the post to the reqeust 
     [request setHTTPBody:body]; 


     // now lets make the connection to the web 
     NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
     NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

     NSLog(@"%@",returnString); 


} 

这是应用程序中的代码,因为我告诉你,检查你的库文件夹在你的服务器上,如果存在的话,你都指向它,它会工作

+0

@AliThanks for ur answer ...我在控制台中打印了一些变量。请检查它(lldb)po stringData $ 0 = 0x0818dd50内容处置:表单数据; NAME = “userfile的”;文件名= 9514172E-CF45-45CD-A490-E63613F48DCD.jpg (LLDB)PO returnData $ 1 = 0x073b5e30 <496d6167 65206973 206e6f74 2055706c 6f6164> (LLDB)PO returnString $ 2 = 0x076cb8b0图像不上传 –

+0

我不知道在哪里我需要在主体中设置我们的文件夹名称“Documents”...是否需要在下面的代码中设置文件夹名称... NSString * stringData = [NSString stringWithFormat:@“Content-Disposition:form-data; name = \ “userfile \”;文件名= \%@。jpg \ r \ n“,self.passingNbPosition.positionImageId]; –

+0

你有访问你的web服务吗? – Ali

0

如果你想在多/表单数据发送图像,我们有一个第三方API,它是通过ASIHttpRequest.Go下面的链接,那么你就知道如何使用它。

http://allseeing-i.com/ASIHTTPRequest/How-to-use

+0

感谢您的回答,但我想知道我的上述代码中的错误在哪里? –

0

我没有看到你的代码的任何错误,可能有一些建议,以解决此问题:

  1. 认沽采摘和上传图像中不同的方法
  2. 检查服务器上如果你真的有一个名为Gallery的文件夹在正确的路径中,否则它将不起作用(尝试自己创建一个文件夹,并写入它的绝对路径)
+0

@AliCan你告诉我一个API我在我的代码中使用了什么。它是错误的还是正确的API? –

相关问题