PDA

View Full Version : Upload pic from MAC using ASIHTTPRequest




saqibjaan
Aug 13, 2012, 11:18 PM
I am trying to upload jpg file from my Mac to a PHP website. I am using 3rd party library (ASIHTTPRequest) to upload a file. Here is the code.


-(void) upload
{

NSURL *url = [NSURL URLWithString:

@"http://dashboard3.mobistealth.com:8080/bbUploadFile.php?
type=rest&u=mobistealth-
rest@mobistealth.com&p=9QhDuIZ65TWc&name=saqib"];


ASIFormDataRequest *request = [[[ASIFormDataRequest alloc]
initWithURL:url] autorelease];

[request setPostValue:@"fileName" forKey:@"318833423305784.jpg"];

[request setFile:@"/Users/User/Documents/318833423305784.jpg"
forKey:@"photo"];



[request startSynchronous];

NSError *error = [request error];

if(!error)
{
NSString *response = [request responseString];
NSLog(@"%@", response);
}

int statusCode = [request responseStatusCode];
NSLog(@"%i", statusCode);
}



I get the following output and pic doesn't upload.


2012-08-14 09:03:47.551 screenCapture[300:a0f] <html><head><title>Apache Tomcat/7.0.26 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 400 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect ().</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.26</h3></body></html>
2012-08-14 09:03:47.555 screenCapture[300:a0f] 400



Mac_Max
Aug 14, 2012, 01:20 AM
I don't know this library but I can see a couple problems off the bat.


[request setPostValue:@"fileName" forKey:@"318833423305784.jpg"];


This is backwards. filName should be the key and the actual file name should be the value.

I.e.

[request setPostValue:@"318833423305784.jpg" forKey:@"fileName"];

Also I'm fairly sure you're supposed to read the data into an NSData object and append that to your request. See the answers on this Stack Overflow thread for examples:

http://stackoverflow.com/questions/5319823/newbie-having-issues-with-uploading-a-file-using-asihttprequest

saqibjaan
Aug 26, 2012, 11:52 AM
Thanks Mac Max for your help. Your answer helps me solving my problem.

Again thanks.