pankaj
pankaj

Reputation: 8348

upload image iphone

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

Upvotes: 0

Views: 632

Answers (3)

Fernando Madruga
Fernando Madruga

Reputation: 1746

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

Upvotes: 1

Janak Nirmal
Janak Nirmal

Reputation: 22726

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.
}

Upvotes: 0

peterept
peterept

Reputation: 4427

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.

Upvotes: 0

Related Questions