0

i am new to iOS application development, i just want to upload an image file using NSMutableURLRequest. The body of the request should be included with the image. I just want to send image as below as java code

 HttpPut mCreatePost = new HttpPut(params[0]+"/data");
 FileEntity fileEntity = new FileEntity(uploadFile, "image/jpeg");
 mCreatePost.setEntity(fileEntity);
 mHttpClient.execute(mCreatePost);

Please help me to write Objective C code as above. Thanks in advance.

Anil Kumar
  • 1,065
  • 1
  • 13
  • 24

1 Answers1

1

Process

  1. Make a NSMutableURLRequest
  2. Set the URL for NSMutableURLRequest
  3. Set HTTP method for NSMutableURLRequest
  4. Set header fields for NSMutableURLRequest
  5. Set HTTP body for NSMutableURLRequest. This will include the image file and any other parameters (sample code below)
  6. Initialise a previously declared NSMutableData object. This is used to store the response from the server (sample code below)
  7. Initialise a previously declared NSURLConnection object and set its delegate. In the sample code I have made the same class as the delegate and implemented the delegate methods.

Sample code

- (void)uploadImage    
    NSString *urlStr = @"your link to the server";

        NSMutableURLRequest *urlRequest = [[[NSMutableURLRequest alloc] init] autorelease];
        [urlRequest setURL:[NSURL URLWithString:urlStr]];

        [urlRequest setHTTPMethod:@"POST"];

        NSString *myboundary = [NSString stringWithFormat:@"---------------------------14737809831466499882746641449"];
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",myboundary];
        [urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];

        NSMutableData *postData = [NSMutableData data];

        NSString *filePath = @"path to file you want to upload";

            NSData *zipData = [NSData dataWithContentsOfFile:filePath];
            if(zipData != nil){
                [postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", myboundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", @"file.zip"]dataUsingEncoding:NSUTF8StringEncoding]];
                [postData appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
                [postData appendData:[NSData dataWithData:zipData]];
            }


        NSString *params =@"Any other parameters you want to send with the file";

            NSLog(@"%@",params);
            [postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", myboundary] dataUsingEncoding:NSUTF8StringEncoding]];
            [postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"params"] dataUsingEncoding:NSUTF8StringEncoding]];
            [postData appendData:[[NSString stringWithFormat:@"%@", params] dataUsingEncoding:NSUTF8StringEncoding]];


        [postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", myboundary] dataUsingEncoding:NSUTF8StringEncoding]];

        [urlRequest setHTTPBody:postData];

        [urlRequest setValue:[SettingsDataStore getSharedInstance].cooike forHTTPHeaderField:@"Cookie"];

        responsedata = [[NSMutableData alloc] initWithData:nil];
        conn =  [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

    }

    //NSURLConnection Delegate

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        [responsedata appendData:data];
    }
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        NSLog(@"error occured %@", [error localizedDescription]);
        [responseData release];
        [conn release];


    }

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {

        //Do something with responseData and release it

        [responseData release];
        [conn release];

    }
Tula
  • 11
  • 3