Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Deck of Cards
idcreateAttachmentForIssueCode
Card
labelObjectiveC
Code Block
borderColorGreen
langjava
titleJSON
NSString *attachmentURL = [[CSS getDefaultAPIURL] stringByAppendingFormat:@"%@/issues/%@/attachments", [CSS getSlug],currentIssueId];
	MultIPartFormCommunication *MPC = [MultIPartFormCommunication sharedSingleton];
	NSString *imageName = [NSString stringWithFormat:@"issueImage%f",[[NSDate date] timeIntervalSince1970]];
        // See the bottom of the page to see the uploadImage method
	if ([MPC uploadImage:issueImage withExtension:@"jpeg" withFileName:imageName ToURLString:attachmentURL]){
                //succesful uploading
            }else{
                //unsuccesful uploading store the issue image in a list:
            }

See the bottom of the page to see the uploadImage method

Card
labelC#


Anchor
getAttachmentListFromIssueCode
getAttachmentListFromIssueCode

...


Anchor
uploadImageCode
uploadImageCode

...

Upload image utility method for ObjectiveC

Deck of Cards
iduploadImageCode
Card
labelObjectiveC
Code Block
borderColorGreen
langjava
titleJSON
- (BOOL)uploadImage:(UIImage*)image withExtension:(NSString*)ext withFileName:(NSString*)fileName ToURLString:(NSString*)urlString{

    CommunicationSingleton *CSS = [CommunicationSingleton sharedSingleton];
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // Method
    [request setHTTPMethod:@"POST"];

    // Set headers
    NSString *boundary = @"AaB03x";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];

    [request setValue:[CSS getAuthorization] forHTTPHeaderField:@"Authorization"];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

    // FORM
    NSData *dataForTestImage = UIImageJPEGRepresentation(image, 1.0);
    NSMutableData *body = [NSMutableData data];

    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@.%@\"\r\n", fileName,ext] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:dataForTestImage];

    // fileName
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"fileName\"\r\n\r\n%@.%@", fileName,ext] dataUsingEncoding:NSUTF8StringEncoding]];

    // type
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"type\"\r\n\r\nimage/%@", ext] dataUsingEncoding:NSUTF8StringEncoding]];

    // size
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"size\"\r\n\r\n%d", [dataForTestImage length]] dataUsingEncoding:NSUTF8StringEncoding]];


    // objectIds
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"objectIds\"\r\n\r\n%@", @""]
dataUsingEncoding:NSUTF8StringEncoding]];

    // End
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPBody:body];

    NSHTTPURLResponse* response;
    NSError* err;
    [NSURLConnection sendSynchronousRequest:request
                          returningResponse:&response
                                      error:&err];

    if ([response statusCode] == 201) {
        return true;
    }else{
        return false;
    }
}