Upload pic from MAC using ASIHTTPRequest

Discussion in 'Mac Programming' started by saqibjaan, Aug 13, 2012.

  1. saqibjaan, Aug 13, 2012
    Last edited by a moderator: Aug 13, 2012

    macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #1
    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.


    Code:
    -(void) upload
    {
    
        NSURL *url = [NSURL URLWithString:
    				   
                      @"http://dashboard3.mobistealth.com:8080/bbUploadFile.php?
                                                 type=rest&u=mobistealth-
                           [email]rest@mobistealth.com[/email]&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
     
  2. macrumors 6502

    Joined:
    Mar 8, 2004
    #2
    I don't know this library but I can see a couple problems off the bat.

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

    I.e.

    [request setPostValue:mad:"318833423305784.jpg" forKey:mad:"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/...es-with-uploading-a-file-using-asihttprequest
     
  3. thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #3
    Thanks Mac Max for your help. Your answer helps me solving my problem.

    Again thanks.
     

Share This Page