2016-07-01 17 views
-1

イメージをサーバーに保存する必要がありますが、イメージは保存されていません。他のものは保存しています。私はこのコードを使用したが、私は何をしたいの結果を得ていないのです...私は http://dev-demo.info.bh-in-15.webhostbox.net/dv/nationalblack/ulpoad/post/imagenamePOSTを使用してイメージがサーバーに保存されないのはなぜですか?

- (IBAction)SaveAction:(id)sender 

{ 
    NSString *businessStr = BusinessField.text; 
    NSString *addStr = AddressField.text; 
    NSString *webStr = websiteField.text; 
    NSString *fbStr = facebookField.text; 
    NSString *twitStr = twitterField.text; 
    NSString *instaStr = instagramField.text; 
    NSString *tumbStr = tumblerField.text; 
    NSString *pintStr = pinterestField.text; 
    NSString *shortStr = shortDescView.text; 
    NSString *longStr = longDescView.text; 
    NSString *categoryStr = categoridddd.text; 
    NSString *subcategoryStr = subcategoryidddd.text; 
    NSString *countStr = CountryLabel.text; 
    NSString *stateStr = StateLabel.text; 
    NSString *cityStr = cityField.text; 
    NSString *NameString = personNameField.text; 
    NSString *emailString = emailField.text; 
    NSString *phoneString = phoneField.text; 


    [self randomStringWithLength:8]; 

    [self randomStringImageWithLength:5]; 

    NSMutableData *body = [NSMutableData data]; 

    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; 

    [parameters setValue:businessStr forKey:@"title"]; 
    [parameters setValue:cityStr forKey:@"city"]; 
    [parameters setValue:stateStr forKey:@"state"]; 
    [parameters setValue:countStr forKey:@"country"]; 
    [parameters setValue:addStr forKey:@"address"]; 
    [parameters setValue:userID forKey:@"memberId"]; 
    [parameters setValue:strName forKey:@"UserName"]; 
    [parameters setValue:@"0" forKey:@"isfeatured"]; 
    [parameters setValue:categoryStr forKey:@"prod_name"]; 
    [parameters setValue:subcategoryStr forKey:@"var_name"]; 
    [parameters setValue:@"0" forKey:@"package_name"]; 
    [parameters setValue:@"0" forKey:@"category"]; 
    [parameters setValue:shortStr forKey:@"shortdesc"]; 
    [parameters setValue:longStr forKey:@"longdesc"]; 
    [parameters setValue:@"35" forKey:@"amount"]; 
    [parameters setValue:@" " forKey:@"catname"]; 
    [parameters setValue:NameString forKey:@"contactperson"]; 
    [parameters setValue:phoneString forKey:@"business_phone"]; 
    [parameters setValue:@" " forKey:@"tags"]; 
    [parameters setValue:@"365" forKey:@"subs_name"]; 
    [parameters setValue:@"35" forKey:@"subs_price"]; 
    [parameters setValue:timeStampStr forKey:@"paydate"]; 
    [parameters setValue:timeStampStr forKey:@"startdate"]; 
    [parameters setValue:endDateStr forKey:@"enddate"]; 
    [parameters setValue:randomString forKey:@"transactionid"]; 
    [parameters setValue:@"ZWZ3QDEyMw==" forKey:@"api_key"]; 
    [parameters setValue:@"123456" forKey:@"api_password"]; 
    [parameters setValue:webStr forKey:@"web_address"]; 
    [parameters setValue:fbStr forKey:@"fb_address"]; 
    [parameters setValue:twitStr forKey:@"tw_address"]; 
    [parameters setValue:instaStr forKey:@"inst_address"]; 
    [parameters setValue:pintStr forKey:@"pt_address"]; 
    [parameters setValue:tumbStr forKey:@"tb_address"]; 

    NSString *boundary = @"------VohpleBoundary4QuqLuM1cE5lMwCy"; 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; 
    [request setHTTPShouldHandleCookies:NO]; 
    [request setTimeoutInterval:30]; 
    [request setHTTPMethod:@"POST"]; 
    NSURL *baseUrl = [NSURL URLWithString:@"http://dev-demo.info.bh-in-15.webhostbox.net/dv/nationalblack/api/addbusiness"]; 

    // set Content-Type in HTTP header 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"]; 

    // add params (all params are strings) 
    for (NSString *param in parameters) { 
     [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]]; 
    } 

    NSData *imageData = UIImageJPEGRepresentation(image1, 1.0); 

    NSString *FileParamConstant [email protected]"ImageFile"; 

    if (imageData) { 
     [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:imageData]; 
     [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    } 
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    // setting the body of the post to the request 
    [request setHTTPBody:body]; 

    // set URL 
    [request setURL:baseUrl]; 

    [NSURLConnection sendAsynchronousRequest:request 
             queue:[NSOperationQueue mainQueue] 
          completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
    { 

     NSString *test = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

     NSLog(@"response : %@",test); 

           NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response; 

           if ([httpResponse statusCode] == 200) { 

            NSLog(@"success"); 
           } 

          }]; 
} 

が私を助けてください、このアドレスに画像を保存する必要があります。私は昨日からこれを試しています。助けをありがとう

+0

最初にiOSの基本を学びます。その後、先に進んでください –

答えて

0
- (IBAction)SaveAction:(id)sender 
{ 
    NSString *strServicePath = [NSString stringWithFormat:@"%@",@"http://dev-demo.info.bh-in-15.webhostbox.net/dv/nationalblack/api/addbusiness"]; 
           NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:strServicePath]]; 
           [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; 
           [request setHTTPShouldHandleCookies:NO]; 
           [request setTimeoutInterval:60]; 
           [request setHTTPMethod:@"POST"]; 

           // NSString *boundary = @"unique-consistent-string"; 
           NSString *boundary = [NSString stringWithFormat:@"Boundary-%@", [[NSUUID UUID] UUIDString]]; 

           // set Content-Type in HTTP header 
           NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
           [request setValue:contentType forHTTPHeaderField: @"Content-Type"]; 

           NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; 
           [parameters setValue:businessStr forKey:@"title"]; 
           [parameters setValue:cityStr forKey:@"city"]; 
           [parameters setValue:stateStr forKey:@"state"]; 
           [parameters setValue:countStr forKey:@"country"]; 
           [parameters setValue:addStr forKey:@"address"]; 
           [parameters setValue:userID forKey:@"memberId"]; 
           [parameters setValue:strName forKey:@"UserName"]; 
           [parameters setValue:@"0" forKey:@"isfeatured"]; 
           [parameters setValue:categoryStr forKey:@"prod_name"]; 
           [parameters setValue:subcategoryStr forKey:@"var_name"]; 
           [parameters setValue:@"0" forKey:@"package_name"]; 
           [parameters setValue:@"0" forKey:@"category"]; 
           [parameters setValue:shortStr forKey:@"shortdesc"]; 
           [parameters setValue:longStr forKey:@"longdesc"]; 
           [parameters setValue:@"35" forKey:@"amount"]; 
           [parameters setValue:@" " forKey:@"catname"]; 
           [parameters setValue:NameString forKey:@"contactperson"]; 
           [parameters setValue:phoneString forKey:@"business_phone"]; 
           [parameters setValue:@" " forKey:@"tags"]; 
           [parameters setValue:@"365" forKey:@"subs_name"]; 
           [parameters setValue:@"35" forKey:@"subs_price"]; 
           [parameters setValue:timeStampStr forKey:@"paydate"]; 
           [parameters setValue:timeStampStr forKey:@"startdate"]; 
           [parameters setValue:endDateStr forKey:@"enddate"]; 
           [parameters setValue:randomString forKey:@"transactionid"]; 
           [parameters setValue:@"ZWZ3QDEyMw==" forKey:@"api_key"]; 
           [parameters setValue:@"123456" forKey:@"api_password"]; 
           [parameters setValue:webStr forKey:@"web_address"]; 
           [parameters setValue:fbStr forKey:@"fb_address"]; 
           [parameters setValue:twitStr forKey:@"tw_address"]; 
           [parameters setValue:instaStr forKey:@"inst_address"]; 
           [parameters setValue:pintStr forKey:@"pt_address"]; 
           [parameters setValue:tumbStr forKey:@"tb_address"]; 

           // post body 
           NSMutableData *body = [NSMutableData data]; 
           [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

           for (NSString *param in parameters) { 
            NSMutableString *strPart = [NSMutableString stringWithFormat:@"%@",[self createPartWithKey:param value:[_params objectForKey:param] boundary:boundary encoding:@"8bit" contentType:@"text/plain" contentFilename:@""]]; 
            [body appendData:[strPart dataUsingEncoding:NSUTF8StringEncoding]]; 
           } 


           NSString *strProfileImageName = [NSString stringWithFormat:@"%@_.jpg",@"ImageFile"]; 
           if (imageData) { 
            [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
            [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=%@\r\n", @"profile_pic",strProfileImageName] dataUsingEncoding:NSUTF8StringEncoding]]; 
            [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
            [body appendData:imageData]; 
            [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
           } 

           [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

           // setting the body of the post to the reqeust 
           [request setHTTPBody:body]; 

           // set the content-length 
           NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]]; 
           [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 

           [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
     if(data != nil) 
     { 
      [HUD hideAnimated:YES]; 
      NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response; 
      NSInteger code = [httpResponse statusCode]; 
      NSLog(@"Status Code: %ld", (long)code); 
      if (code == 200) { 
       NSError *error; 
       id responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; 
      } 
     } 
     }); 
} 
関連する問題