1

I have already gone through some codings, and I found some interrupts in uploading.

I want to upload six images to a server folder and some text to server database using ASIHTTPRequest. Any sample code please, thanks for spending time for my question.

Kanan Vora
  • 2,124
  • 1
  • 16
  • 26
Nazik
  • 8,696
  • 27
  • 77
  • 123
  • 1
    consider doing some reading, trying out some code, and, if you hit a snag, asking a specific question. Expecting the community to do your work for you is not appropriate – QED Mar 21 '12 at 08:27
  • 1
    I already gone through some codings,and found some interrupts in uploading, thats why went for generic question @psoft – Nazik Mar 21 '12 at 09:55

3 Answers3

3

Here is the code for image uploading, you can use it

-(void)uploadImage

{
    UIImage *image = [UIImage imageWithName:@"sample.jpeg"]; 
    NSData *imageData = UIImageJPEGRepresentation(image, 90);
    NSURL *url = [NSURL URLWithString:@"http://your-url/upload.php"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setDelegate:self];
    [request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];



[request startAsynchronous];

}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *responseString = [request responseString];
    NSLog(@"response: %@", responseString);

    // Use when fetching binary data
    NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
}
Shahrukh A.
  • 1,071
  • 3
  • 11
  • 20
2

For uploading images to the server you need the following code:

NSURL *url = [NSURL URLWithString:@"http://www.xyz.com/UploadImage.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
request.requestMethod = @"POST";
NSString *fileName = @"iphone.jpg";
[request addPostValue:fileName forKey:@"name"];

// Upload an image
UIImage *img = [UIImage imageNamed:fileName];
NSData *imageData = UIImageJPEGRepresentation(img, 90);
[request setData:imageData withFileName:fileName andContentType:@"image/jpeg" forKey:@"image"];
[request setDelegate:self]; 
[request startAsynchronous];

For sending text to the server, you just need to append the text with the POST method like:

[request appendPostData:[@"This is my data" dataUsingEncoding:NSUTF8StringEncoding]];

Cheers!!!

Kanan Vora
  • 2,124
  • 1
  • 16
  • 26
0

For text, you can use this

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[@"This is my data" dataUsingEncoding:NSUTF8StringEncoding]];
// Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody:
[request setRequestMethod:@"PUT"];

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

Shahrukh A.
  • 1,071
  • 3
  • 11
  • 20