Upload image to the PHP server from iOS

后端 未结 6 666
天涯浪人
天涯浪人 2020-11-30 22:02

I know this question has been asked earlier as well but my issue is a bit different.

I want to upload an image to the PHP server and i want to send more parameters a

6条回答
  •  感情败类
    2020-11-30 22:51

    -(void)uploadImage
    {
    
    NSString *mimetype = @"image/jpeg";
    NSString *myimgname = _txt_fname.text; //@"img"; //upload image with this name in server PHP FILE MANAGER
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *imageDataa = UIImagePNGRepresentation(chooseImg.image);
    NSDictionary *parameters =@{@"fileimg":defaults }; //@{@"uid": [uidstr valueForKey:@"id"]};
    
    AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
    //here post url and imagedataa is data conversion of image  and fileimg is the upload image with that name in the php code
    NSMutableURLRequest *request =
    [serializer multipartFormRequestWithMethod:@"POST" URLString:@"http://posturl/newimageupload.php"
                                    parameters:parameters
                     constructingBodyWithBlock:^(id formData) {
                         [formData appendPartWithFileData:imageDataa
                                                     name:@"fileimg"
                                                 fileName:myimgname
                                                 mimeType:mimetype];
                     }];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    AFHTTPRequestOperation *operation =
    [manager HTTPRequestOperationWithRequest:request
                                     success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                         NSLog(@"Success %@", responseObject);
                                         [uploadImgBtn setTitle:@"Uploaded" forState:UIControlStateNormal];
                                         [chooseImg setImage:[UIImage imageNamed:@"invoice-icon.png"]];
    
                                         if([[responseObject objectForKey:@"status"] rangeOfString:@"Success"].location != NSNotFound)
                                         {
                                             [self alertMsg:@"Alert" :@"Upload sucess"];
                                         }
    
                                     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                         NSLog(@"Failure %@", error.description);
                                         [chooseImg setImage:[UIImage imageNamed:@"invoice-icon.png"]];
                                         uploadImgBtn.enabled = YES;
    
                                     }];
    
    // 4. Set the progress block of the operation.
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
    
        float myprog = (float)totalBytesWritten/totalBytesExpectedToWrite*100;
        NSLog(@"Wrote %f ", myprog);
    }];
    
    // 5. Begin!
    [operation start];
    
    }
    

提交回复
热议问题