0

I am trying to upload uiimage from my phone application to server using php. But some how it is not working. Can someone please help me with that. My Code:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
            request.delegate = self;
            request.tag = 33;
            [request setPostValue:[NSString stringWithFormat:@"%@.png",responseString] forKey:@"name"];
            NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
            NSString *docDirectory = [sysPaths objectAtIndex:0];
            NSString *filePath = [NSString stringWithFormat:@"%@/MyImage.png", docDirectory];
            [request setFile:filePath forKey:@"photo"];

            [request startAsynchronous];
            [url release];
            NSError *error = [request error];
            if (!error) {
                NSString *response = [request responseString];
                NSLog(@"aResponse:%@",response);

            }

if(move_uploaded_file($_FILES['photo']['tmp_name'],$target_path)) {
    echo "The file ". basename( $_FILES['photo']['tmp_name'])." has been uploaded";
} 
else {
    echo "There was an error uploading the file, please try again!";
}

>

Got Following error when debugged with nszombie in a file of ASI Request

* -[CFString release]: message sent to deallocated instance 0xd1dfd80

pankaj
  • 7,878
  • 16
  • 69
  • 115

3 Answers3

1

Don't release the URL: request will run asynchronously so it will not have been done with it by the time you release it.

Fernando Madruga
  • 1,746
  • 13
  • 11
0

You're setting the filename but not the file contents:

[request setData: UIImageJPEGRepresentation(imageView.image, 1.0) withFileName:filename andContentType:@"image/jpeg"

See this Stackoverflow answer.

Community
  • 1
  • 1
peterept
  • 4,407
  • 23
  • 32
0

You can check my answer here and it worked so may be for you too ?

NSData *imgData = UIImagePNGRepresentation(YourUIImageObject);

NSURL *url = @"yourURL";

ASIFormDataRequest *currentRequest = [ASIFormDataRequest requestWithURL:url];
[currentRequest setPostFormat:ASIMultipartFormDataPostFormat];
[currentRequest setRequestMethod:@"POST"];
[currentRequest addData:imgData withFileName:@"file" andContentType:@"image/png" forKey:@"yourFileNameOnServer"]; //This would be the file name which is accepting image object on server side e.g. php page accepting file
[currentRequest setDelegate:self];
[currentRequest setDidFinishSelector:@selector(uploadImageFinished:)];
[currentRequest setDidFailSelector:@selector(uploadImageFailed:)];  
[currentRequest startSynchronous];


-(void)uploadImageFinished:(ASIHTTPRequest*)request
{
     //Your request successfully executed. Handle accordingly.
}

-(void)uploadImageFailed:(ASIHTTPRequest*)request
{
     //Your request failed to execute. Handle accordingly.
}
Community
  • 1
  • 1
Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
  • hi thanks for you help. I have tried it and I am not getting any error but file is still not being uploaded. Can you please also help me with server side code? – pankaj Mar 15 '12 at 13:11