2014-10-08 116 views
-2

我有一個應用程序,我在上傳一些數據到Web服務器。上傳圖像到網絡服務器從ios到PHP

這是我從谷歌的鏈接中獲得的代碼。

UIImage * img = [UIImage imageNamed:@"register_btn.png"]; 

    NSData *imageData = UIImageJPEGRepresentation(img, 90); 
    NSString *urlString = @"http://tes.in/testservices/User.php"; 

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

    NSString *boundary = @"------------------------1473780983146499882746641449"; 
    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--%@r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data;filename=\a.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Type: application/json\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]; 
     //profile_photo_link 
    NSLog(@"%@",returnString); 

但我沒有得到什麼,我需要在代碼中傳遞。

我的照片上傳參數是「photo_link」。我已經給出了PHP web服務的鏈接。

我知道這裏有很多問題,但我不知道在哪裏傳遞我的參數以及如何檢查請求字符串。

如果可能的話,請任何人都可以給我PHP的代碼來解碼圖像。

請幫幫我。

在此先感謝。

+0

你有沒有想過使用AFNETWorking?它大大簡化了這樣的請求 – Smiless 2014-10-08 07:39:13

+0

如果你可以給我這個代碼,那會很好。我已經給出了我的參數和web服務的鏈接。 – 2014-10-08 07:40:34

+0

爲什麼downvote?這是一個法律問題,具有所有的侷限性和關於程序設計的問題。那麼爲什麼人們會倒下。只是不明白。 – 2014-10-09 04:18:19

回答

1

試試這個代碼:

-(void)uploadImageOnServer:(NSData *)imageData forId:(NSString *)imageId 
{    
    NSURL *strURL = @"http://webserver.com/....";//give your URL here 

    // setting up the request object now 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ; 
    [request setURL:strURL]; 
    [request setHTTPMethod:@"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=\"Documents\"; filename=\%@.jpg\r\n",imageId]; 
    [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 request 
    [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

感謝您的回覆。什麼是util和方法?我是否需要導入任何框架? – 2014-10-08 07:46:12

+0

@PatelManthansorry bro,就像發佈API的操作一樣。請忽略此並檢查我的更新答案。 – 2014-10-08 07:48:49

+0

@PatelManthan你試過我的答案,它對你有用嗎? – 2014-10-08 09:18:07