2013-11-02 20 views
1

我正在嘗試上傳圖片以及一些文字。我能夠張貼文本,但不知何故圖像不能到達服務器。以下是我的代碼:在單個帖子請求中上傳圖片和一些其他參數

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
     NSString *password = [[Utilities sharedConfiguration] sha1:txtPassword.text]; 
     NSString *post = [NSString stringWithFormat:@"id_sender=%@&token=%@&array_receiver=%@&message=%@&time_allowed=%@&password=%@",[defaults objectForKey:@"id_user"],[defaults objectForKey:@"token"],selectedContact,txtMessage.text,txtTime.text,password]; 
     NSLog(@"post %@",post); 
     NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
     NSMutableData *data = [NSMutableData dataWithData:postData]; 
     //NSMutableData *data = nil; 
     if(myimage!=nil){ 
      NSString *boundary = @"---------------------------14737809831466499882746641449"; 
      [data appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
      [data appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filetype=\"image/png\"; filename=\"image.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
      [data appendData:[[NSString stringWithFormat:@"Content-Type: image/png\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
      [data appendData:[NSData dataWithData:UIImagePNGRepresentation(myimage)]]; 
     } 
     NSString *postLength = [NSString stringWithFormat:@"%d", [data length]]; 

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
     [request setURL:[NSURL URLWithString:@"https://www.myurl.com/send_message_17294.php"]]; 
     [request setHTTPMethod:@"POST"]; 
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
     [request setHTTPBody:data]; 

     [MBProgressHUD showHUDAddedTo:self.view animated:TRUE]; 
     [NSURLConnection connectionWithRequest:request delegate:self]; 

有人能幫我嗎?

問候 潘卡

+0

可能的重複:http://stackoverflow.com/questions/936855/file-upload-to-http-server-in-iphone-programming? –

+0

該網址顯示如何只上傳圖片。我想上傳圖片和一些文字。兩者在相同的帖子請求中。 – pankaj

+0

我想你或者需要多部分,就像那個例子所示。 。或者你可以base64編碼圖像數據並將其嵌入到一個字段中。 –

回答

2

您可以使用AFNetworking所以您不必到Digg成多部分請求的格式。

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://hostport"]]; 
NSDictionary *simpleParams = @{ @"key": @"value" }; 
NSMutableURLRequest* request = [client multipartFormRequestWithMethod:@"POST" path:@"/path" parameters: simpleParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 

     [formData appendPartWithFileData:fileData 
            name:@"paramName" 
           fileName:@"filename.png" 
           mimeType:@"image/png"]; 
    }]; 

然後根據預期的響應創建一個操作。例如,如果你期望JSON:

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 

    // handle success 

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 

    // handle error 

}]; 

你可以在AFNetworking documentation得到更多的信息。

-1

試試這個:它是一個同步請求。而圖像是的base64字符串格式

NSString * param = [NSString stringWithFormat:@"%@=%@&%@=%@&%@=%@", 
           @"uphoto",imageData, @"category",categoryName, @"name",FIRSTNAME]; // parameters 

    NSData *postData = [param dataUsingEncoding:NSUTF8StringEncoding]; 
    NSURL *url = [NSURL URLWithString:@"http://www. ..."]; 
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; 
    [req setHTTPMethod:@"POST"]; 
    [req setValue:[NSString stringWithFormat:@"%d", postData.length] forHTTPHeaderField:@"Content-Length"]; 
    [req setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [req setHTTPBody:postData]; 

    NSError *error = nil; 
    NSHTTPURLResponse *res = nil; 

    NSData *responceData = [NSURLConnection sendSynchronousRequest:req returningResponse:&res error:&error]; 
    if (error) 
    { 
     //handle error   
     return nil; 
    } 
    else 
    { 

    } 
+0

爲什麼投票? – Maulik

相關問題