Reputation: 1751
I need to send my image to salesforce through my iphone app.I have tried those things converted the image--> bytes-->base64 encoding then store the sfdc (Rich data field), it's done perfectly, but i need save as an image .Here are My codes given below (it's not working) Guides me how to achieve that
NSData *imageData = UIImagePNGRepresentation(imageView.image);
NSString *boundary = @"---------------------------14737809831466499882746641449";
SFRestRequest *request = [[SFRestRequest alloc] init];
[request setDelegate:self];
[request setEndpoint:kSFDefaultRestEndpoint];
[request setMethod:SFRestMethodPOST];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content- Disposition: form-data; name=\"entity_document\"; filename=\"%@\"\r\n",@"Test.png"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: multipart/form-data\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Description :\"%s\"\r\n","Marketing brochure for Q1 2011"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Keywords :\"%s\"\r\n","marketing,sales,update"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"FolderId :\"%s\"\r\n","005D0000001GiU7"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Name :\"%s\"\r\n","Marketing Brochure Q1"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *attbody = [NSString stringWithFormat:@"{\"body\" : {\"Name\" :[{ \"type\" : \"image/jpeg\", \"image\" : \"%@\"}] } }",body];
[request setPath:[NSString stringWithFormat:@"/v23.0/sobjects/Document/"]];
[request setQueryParams:(NSDictionary *)[SFJsonUtils objectFromJSONString:attbody]];
[[SFRestAPI sharedInstance] send:request delegate:self];
Is there any class(Apex) needed to process my request?
Upvotes: 1
Views: 1686
Reputation: 1022
To make it so the View function works for images in Attachments in the Salesforce Admin GUI you want the ContentType field as well:
UIImage *testImg = [UIImage imageNamed:@"someImageName"];
NSData *testImgData = UIImagePNGRepresentation(testImg);
NSString *b64 = [testImgData base64Encode]; // base64Encode declared in NSData+SFAdditions.h
NSDictionary *fields = @{
@"Name": @"someImage.png",
@"Body": b64,
@"ParentId": @"00Q50000016TuGIEA0", // of course put your Attachment Id here instead of mine
@"ContentType": @"image/png"
};
SFRestRequest *attachmentRequest = [[SFRestAPI sharedInstance] requestForCreateWithObjectType:@"Attachment" fields:fields];
[[SFRestAPI sharedInstance] send:attachmentRequest delegate:self];
Upvotes: 0
Reputation: 11
NSData *myData = [[NSData alloc] init];//init with your file content
NSString *b64 = [myData base64EncodedString];
NSDictionary *fields = @{
@"Name": @"Document Name.ext",
@"Body": b64,
@"ParentId": recordId
};
attachmentRequest = [[SFRestAPI sharedInstance] requestForCreateWithObjectType:@"Attachment" fields:fields];
[[SFRestAPI sharedInstance] sendRESTRequest:attachmentRequest failBlock:^(NSError *e) {
NSLog(@"Error");
} completeBlock:^(id dict){
NSLog(@"Uploaded");
}];
Upvotes: 1
Reputation: 1751
I have created apex rest classes to recieve the binary data and then save to SFDC (image saved in Documents).Here are the tutorial i referred .
http://blogs.developerforce.com/developer-relations/2011/09/using-binary-data-with-rest.html
Also here are the tutorial How to access the apex webservice through my ios.
http://techblog.appirio.com/2013/07/rest-with-apex-service-in-native-ios.html
Upvotes: 0
Reputation: 8255
If you just want to upload binary data to an attachment and/or document then you should not need to write an Apex class at all, you should be able to do everything via the REST API.
Apex classes as shown in the blog post linked are just for creating custom webservices. You can go this route if you choose but it's definitely not required for what you're trying to achieve.
Upvotes: 0