4

I'm uploading an image as an attachment to an object in Salesforce via Rest API v32. My HTTP request body looks like so:

--boundary_string_1416597802788
Content-Disposition: form-data; name="entity_attachment";
Content-Type: application/json

{"ContentType":"image/png;base64","Name":"Signature","Description":"Signature signed on November 21st 2014 1:23 PM","ParentId":"a02J000000DsXDQIA3","IsPrivate":false}

--boundary_string_1416597802788
Content-Type: image/png;base64
Content-Disposition: form-data; name="Body"; filename="filler"

iVBORw0KGgoAAAANSUhEUgAAAr0AAACuCAYAAAA7+bXEAAAMmUlEQVR4Xu3aQYuVZRgGYI/TLAQjl0Ebt60aUFcF+QeCoB/hL+iftPFHCIG0n9aOMhuhdrapli6CktTT+4EzmFjPO4eZc7wfL0FMn2++732u+8tuTrO65AcBAgQIECBAgACB5gKr5vtZjwABAgQIECBAgMAlpddLQIAAAQIECBAg0F5A6W0fsQUJECBAgAABAgSUXu8AAQIECBAgQIBAewGlt33EFiRAgAABAgQIEFB6vQMECBAgQIAAAQLtBZTe9hFbkAABAgQIECBAQOn1DhAgQIAAAQIECLQXUHrbR2xBAgQIECBAgAABpdc7QIAAAQIECBAg0F5A6W0fsQUJECBAgAABAgSUXu8AAQIECBAgQIBAewGlt33EFiRAgAABAgQIEFB6vQMECBAgQIAAAQLtBZTe9hFbkAABAgQIECBAQOn1DhAgQIAAAQIECLQXUHrbR2xBAgQIECBAgAABpdc7QIAAAQIECBAg0F5A6W0fsQUJECBAgAABAgSUXu8AAQIECBAgQIBAewGlt33EFiRAgAABAgQIEFB6vQMECBAgQIAAAQLtBZTe9hFbkAABAgQIECBAQOn1DhAgQIAAAQIECLQXUHrbR2xBAgQIECBAgAABpdc7QIAAAQIECBAg0F5A6W0fsQUJECBAgAABAgSUXu8AAQIECBAgQIBAewGlt33EFiRAgAABAgQIEFB6vQMECBAgQIAAAQLtBZTe9hFbkAABAgQIECBAQOn1DhAgQIAAAQIECLQXUHrbR2xBAgQIECBAgAABpdc7QIAAAQIECBAg0F5A6W0fsQUJECBAgAABAgSUXu8AAQI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kDprYTMCRAgQIAAAQIE4gWU3vgILUCAAAECBAgQIFAJKL2VkDkBAgQIECBAgEC8gNIbH6EFCBAgQIAAAQIEKgGltxIyJ0CAAAECBAgQiBdQeuMjtAABAgQIECBAgEAloPRWQuYECBAgQIAAAQLxAkpvfIQWIECAAAECBAgQqASU3krInAABAgQIECBAIF5A6Y2P0AIECBAgQIAAAQKVgNJbCZkTIECAAAECBAjECyi98RFagAABAgQIECBAoBJQeishcwIECBAgQIAAgXgBpTc+QgsQIECAAAECBAhUAkpvJWROgAABAgQIECAQL6D0xkdoAQIECBAgQIAAgUpA6a2EzAkQIECAAAECBOIFlN74CC1AgAABAgQIECBQCSi9lZA5AQIECBAgQIBAvIDSGx+hBQgQIECAAAECBCoBpbcSMidAgAABAgQIEIgXUHrjI7QAAQIECBAgQIBAJaD0VkLmBAgQIECAAAEC8QJKb3yEFiBAgAABAgQIEKgElN5KyJwAAQIECBAgQCBeQOmNj9ACBAgQIECAAAEClYDSWwmZEyBAgAABAgQIxAsovfERWoAAAQIECBAgQKASUHorIXMCBAgQIECAAIF4AaU3PkILECBAgAABAgQIVAJKbyVkToAAAQIECBAgEC+g9MZHaAECBAgQIECAAIFKQOmthMwJECBAgAABAgTiBZTe+AgtQIAAAQIECBAgUAkovZWQOQECBAgQIECAQLyA0hsfoQUIECBAgAABAgQqAaW3EjInQIAAAQIECBCIF1B64yO0AAECBAgQIECAQCWg9FZC5gQIECBAgAABAvECSm98hBYgQIAAAQIECBCoBJTeSsicAAECBAgQIEAgXkDpjY/QAgQIECBAgAABApWA0lsJmRMgQIAAAQIECMQLKL3xEVqAAAECBAgQIECgElB6KyFzAgQIECBAgACBeAGlNz5CCxAgQIAAAQIECFQCSm8lZE6AAAECBAgQIBAvoPTGR2gBAgQIECBAgACBSkDprYTMCRAgQIAAAQIE4gWU3vgILUCAAAECBAgQIFAJKL2VkDkBAgQIECBAgEC8gNIbH6EFCBAgQIAAAQIEKgGltxIyJ0CAAAECBAgQiBdQeuMjtAAB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BWQOmdlXIdAQIECBAgQIBArIDSGxudgxMgQIAAAQIECMwKKL2zUq4jQIAAAQIECBCIFVB6Y6NzcAIECBAgQIAAgVkBpXdWynUECBAgQIAAAQKxAkpvbHQOToAAAQIECBAgMCvwD2zg269ST+CHAAAAAElFTkSuQmCC

--boundary_string_1416597802788--

Please note that the encoded base64 image is not sensitive

The upload completes successfully, and I can verify that the base64 encoded data is now saved in Salesforce. However, I cannot view the attachment on Salesforce. When trying to "view" the image, I am taken to a url like ..../servlet/servlet.FileDownload?file=00PJ0000001tmb3 and the image displays a broken image icon (in Firefox, it tells me that the image cannot be displayed because it contains errors).

After some investigation, I realized that the image is not displaying because it is still in the base64 format, even after it has been uploaded. I assumed that I would need to upload the file in base64 format, but this assumption appears to be wrong, OR Salesforce is not properly decoding it on their side.

What I've tried:

  • I tried playing around with the content types, and removed the ;base64 part and uploaded the plain binary. This didn't work, as in the image is not dsiplayable via Salesforce
  • I added a .png extension to the filename="".

Update
Based on Ketich C's answer, I tried adding the Content-Transfer-Encoding: base64 header, but that didn't work, and causes Salesforce to throw an exception upon upload.

5

Based on my research, I have concluded that at the time of this writing (using REST API v32), Salesforce does not decode uploaded base64 encoded data for Attachments. This is a bit confusing, since the Attachments documentation specifies the following:

The API sends and receives the binary file attachment data encoded as a base64Binary data type. Prior to creating a record, client applications must encode the binary attachment data as base64. Upon receiving a response, client applications must decode the base64 data to binary (this conversion is usually handled for you by the SOAP client).

The difficult part is uploading an attachment to match Salesforce's specific multipart/form-data template. Without going into the exhausing details, the only I found to do this was to manually build the HTTP request body, and put the entire body in an ArrayBuffer, which the browser will not UTF-8 encode. This answer on StackOverflow really paved the way for this fix.

// {
//   Body: base64EncodedData,
//   ContentType: '',
//   Additional attachment info (i.e. ParentId, Name, Description, etc.)
// }

function uploadAttachment (objectData, onSuccess, onError) {
    // Define a boundary
    var boundary = 'boundary_string_' + Date.now().toString();
    var attachmentContentType = !app.isNullOrUndefined(objectData.ContentType) ? objectData.ContentType : 'application/octet-stream';

    // Serialize the object, excluding the body, which will be placed in the second partition of the multipart/form-data request
    var serializedObject = JSON.stringify(objectData, function (key, value) { return key !== 'Body' ? value : undefined; });

    var requestBodyBeginning = '--' + boundary
        + '\r\n'
        + 'Content-Disposition: form-data; name="entity_attachment";'
        + '\r\n'
        + 'Content-Type: application/json'
        + '\r\n\r\n'
        + serializedObject
        + '\r\n\r\n' +
        '--' + boundary
        + '\r\n'
        + 'Content-Type: ' + attachmentContentType
        + '\r\n'
        + 'Content-Disposition: form-data; name="Body"; filename="filler"'
        + '\r\n\r\n';

    var requestBodyEnd =
        '\r\n\r\n'
        + '--' + boundary + '--';

    // The atob function will decode a base64-encoded string into a new string with a character for each byte of the binary data.
    var byteCharacters = window.atob(objectData.Body);

    // Each character's code point (charCode) will be the value of the byte.
    // We can create an array of byte values by applying .charCodeAt for each character in the string.
    var byteNumbers = new Array(byteCharacters.length);

    for (var i = 0; i < byteCharacters.length; i++) {
        byteNumbers[i] = byteCharacters.charCodeAt(i);
    }

    // Convert into a real typed byte array. (Represents an array of 8-bit unsigned integers)
    var byteArray = new Uint8Array(byteNumbers);

    var totalRequestSize = requestBodyBeginning.length + byteArray.byteLength + requestBodyEnd.length;

    var uint8array = new Uint8Array(totalRequestSize);
    var i;

    // Append the beginning of the request
    for (i = 0; i < requestBodyBeginning.length; i++) {
        uint8array[i] = requestBodyBeginning.charCodeAt(i) & 0xff;
    }

    // Append the binary attachment
    for (var j = 0; j < byteArray.byteLength; i++, j++) {
        uint8array[i] = byteArray[j];
    }

    // Append the end of the request
    for (var j = 0; j < requestBodyEnd.length; i++, j++) {
        uint8array[i] = requestBodyEnd.charCodeAt(j) & 0xff;
    }

    return $j.ajax({
        type: "POST",
        url: salesforceUrl,
        contentType: 'multipart/form-data' + "; boundary=\"" + boundary + "\"",
        cache: false,
        processData: false,
        data: uint8array.buffer,
        success: onSuccess,
        error: onError,
        beforeSend: function (xhr) {
            // Setup OAuth headers...
        }
    });
};
  • There is an error in the above answer: var requestBodyEnd = '\r\n\r\n' + '--' + boundary + '--'; There is no need for the second \r\n it will cause an additional byte to be included in the call, which causes Open Office Documents to be corrupted. – user17334 Feb 23 '15 at 21:00
1

Examples I have seen such as Multi-Part MIME Messages use a separate header for the transfer encoding:

Content-Type: image/png
Content-Transfer-Encoding: base64

I suggest you try the separate Content-Transfer-Encoding header.

  • Content-Transfer-Encoding isn't relevant to HTTP unfortunately (at least, the note in section 14.15 says so). Trying to put that in the body of the request, where some of the examples had it, throws an exception on Salesforce's side. – contactmatt Nov 22 '14 at 0:22
  • @contactmatt So not in the spec or implementation - consistently wrong at least... Not sure its relevant but in case you haven't seen it there is Salesforce / Apex POST Mutipart/form-data with HttpRequest. – Keith C Nov 22 '14 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.