Hot answers tagged

24

This is how I send attachments. All credits(& explanation) http://enreeco.blogspot.in/2013/01/salesforce-apex-post-mutipartform-data.html @Future(callout=true) public static void uploadFile(Blob file_body, String file_name, String reqEndPoint){ String boundary = '----------------------------741e90d31eff'; String header = '--'+...


16

Fix for occasional corruption when uploading a file from Salesforce using multipart mime. The Salesforce solutions for multipart mime so far have left one problem outstanding, in that very occasionally you will get corruption in the last few bytes of your data. The fix below resolves this issue, I'm posting it here as I'm extremely grateful to have been ...


16

It is possible to do it by converting Blob to Hex using EncodingUtil.convertToHex() method, concat the Hex Sequence and then convert concatenated Hex string back to Blob using EncodingUtil.ConvertFromHex() method. Blob b1 = Blob.valueOf('s1'); Blob b2 = Blob.valueOf('s2'); Blob b3 = Blob.valueOf('s3'); String combinedDataAsHex = EncodingUtil.convertToHex(...


13

String.valueOf returns Blob[X] for Blob values. I believe it does it this way because System.debug internally uses String.valueOf, and debugging a potentially non-Unicode stream into a Unicode String could break things. In other words, you can't reliably use String.valueOf unless you can also use it in System.debug and get the output you expect. String....


12

Instead of toString, you need to actually use EncodingUtil.convertToHex to get the output you expect: String myString = 'Some String'; system.debug(myString); Blob myBlob = Blob.valueOf(myString); system.debug(myBlob); Blob md5hash = Crypto.generateDigest('MD5', myBlob); System.debug(md5hash); System.debug(EncodingUtil.convertToHex(md5hash)); Blob#toString ...


10

You can use the setBodyAsBlob(Blob someBlob) method on HttpRequest to send a raw blob in an HTTP request. If you need to send a mime multi-part request, then that's harder as there's no good way to glue together the string mime part headers and the binary data (you can at this point, base64 encode the payload to work around that).


10

You can use the string repeat method to build a long string, and then pass that to a Blob. Blob data = Blob.valueOf('a'.repeat(5242880));


8

For servers that accept a Transfer-Encoding header, you can set the appropriate request header and send the data as Base64-encoded. Not all servers support this mode, however. It is also possible to send it as a multipart form message and set the Content-Transfer-Encoding header appropriately for servers that accept multipart form data with base64 encoding. ...


8

Based on the error message you get when you attempt to retrieve the body of an attachment: Visualforce Remoting Exception: No serializer found for class common.udd.object.EncryptableFfxBlobField$DeferredEncryptableFfxBlobValueImpl It would appear that you can't work with blob fields in Visualforce Remote Objects. I'll see if I can find an official ...


7

You must use base-64 encoding if you want to do it this way. The reason why is that internally, Lightning uses JSON, so passing binary data through either way may have unexpected results. If necessary, you might need to return the binary data through several round trips to the server. Alternatively, just make an XMLHttpRequest directly to "/resource/...


6

You need to specify the file extension when giving name to the attachment. a.Name = 'attachment.png'; Or alternatively set the ContentType attribute - Attachment Object Update: Your string is already base64 encoded, so you'll need to strip off the first part (the metadata of the file) and base64 decode the rest (the content): a.Body = ...


6

You have somehow embed the binary data into the field. Here's one way: trigger Update_image on FeedItem (after insert) { id Travelid ; blob img; // Just binary data, thanks! List<Object__c> lst = new List<Object__c>(); For(FeedItem f : Trigger.new){ Travelid = f.parentid; img = f.ContentData; ...


5

There's no way to combine PDFs as such. What you would need to do is generate your PDF in a different way, i.e. from a List as opposed to the single record. You can utilise the visualforce that you have already, and place it in an <apex:repeat/> block. For example, instead of e.g. <apex:page standardController="Account" renderAs="pdf"> <...


5

You will need to set the Mime Content-Type of the attachment to match that of the ContentVersion. This will tell your browser/mail client how to open the attachment. The Mime Type for the ContentVersion is in FileType. You can set the Mime Type on the EmailFileAttachment using setContentType(). efa = new Messaging.EmailFileAttachment(); efa.setFileName(cv....


5

Did you set the contentType as application/pdf? I tried this following code and it is working for me. String jsonStr = '<Base64 String>'; Attachment attach = new Attachment(); attach.contentType = 'application/pdf'; attach.name = 'myfile.pdf'; attach.parentId = '0010H00002DW2Ds'; attach.body = EncodingUtil.base64Decode(jsonStr); insert attach;


5

You can't use both setBody and setBodyAsBlob in the same request. Also, if you're using multipart/form-data, you have to build a multipart payload. That means that you'll need to do something like the following: String body = ''; String boundary = 'ContentThatShouldNotAppearInTheBody'; Map<String, String> params = new Map<String, String> { '...


5

For ContentVersion, the property that contains the blob data is 'VersionData'. Line 6 should be changed to: controller.file.VersionData = Blob.valueOf('populated'); See more here: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentversion.htm


5

Alas, Salesforce doesn't support custom fields of the Blob type. Internally SF use Blob fields though (e.g. attachments, content files). Anyways, you have several options here. (1) Save your Blob as an attachment (or a content file) and relate it to a record. (2) Use a text area (long) custom field and store your blob as base64 encoded text. In this case you ...


5

We had the same problem and we opened a case: we've been told that the LockerService whitelisted the following MIME Types: application/octet-stream application/json video/ audio/ image/ font/ text/plain text/markdown In our case we changed all the MIME Type in our code to "text/plain" and our code still worked fine. So you can try to change your code to: ...


5

The equals operator == does work as expected with Blob, so there's no reason to jump through extra hoops, such as your eq method. However, one possibility is that you may have had a different case in your code; since Blobs are binary data, they are case-sensitive in respect to equality: Blob a = blob.valueof('hello world'), b = blob.valueof('Hello world'...


5

Aside from plain text files (e.g. source code or README.TXT type files), most files follow an internal structure. This means that two similar files (e.g. two GIF or DOCX) being "added together" would result in an invalid file structure. Let me give you an example using plain text; we'll choose XML. File A <?xml version="1.0" encoding=&...


4

Declare an Id variable. In the constructor, retrieve the Id of the record as follows: controller.getId(); With this Id, try to insert the attachment record. Change VFP code like this: <apex:inputfile value="{!myfile.body}" filename="{!myfile.Name}" /> One more point use attachment code like this, private Attachment myfile; public Attachment ...


4

Proper multipart/form-data POST requests from Apex are tricky. As @sfdcfox mentioned, you need to do some very careful manipulations between the 8-bit bytes of the input file and the 6 bit groups used by Base64 encoding. I've written about this in some depth in Steps required to support POSTing multipart/form-data Content-Type from Apex. You might find it ...


4

Use EncodingUtil.base64Encode for the contains for the Blob. Refer EncodingUtil Class String str = 'myString'; Blob b = Blob.valueOf(str); System.debug(b); //it returns Blob[8] String base64String = EncodingUtil.base64Encode(b); System.debug('encoded string=' + base64String); //it returns bXlTdHJpbmc= System.debug('decoded string=' + EncodingUtil....


3

There are a number of AppExchange packages that can generate a PDF using a template- Conga, Drawloop, and Thunderhead are three well known ones. Generally these templates are built in Word or another editor though - not HTML or Visualforce. Document generation for most of these products is "ad-hoc" - users click on a button in Salesforce to generate the ...


3

I know this is four months old, but I just ran into this myself, and may have a reason as to why this occurs. There is a very small blurb at the bottom of this page on the developers site : You can access all of the fields using a query() call. However, you can't receive the Body field for multiple records in a single query() call. If your query returns ...


3

If you mean you want to look at the bytes that make up the file using Apex code you can't directly. You can turn it into a base64 string using EncodingUtil.base64Encode but as the string characters then don't align with the byte boundaries it is very hard work to do anything useful (and you are likely to run into CPU and heap governor limits). So if you ...


3

Instead counter use b.size() for(integer i = 0; i <b.size(); i++) { Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); efa.setFileName('attachment.pdf'); efa.setBody(b[i]); lstFileAttachments.add(efa); }


3

Does the Restful webservice require a multi-part request? If not, your could use HttpRequest.setBodyAsBlob() to POST the raw Blob. See also: Sending a blob from Salesforce in a HTTP request? Rather than using attachment.body.toString() to convert the Blob to a string. Try: base64Encode(Blob) body+= EncodingUtil.base64Encode(attachment.body);


3

File upload between a browser and a HTTP server encodes the data using "multipart/form-data" encoding (see e.g. http://www.rfc-editor.org/rfc/rfc1867.txt) and this allows the form fields and the file data being uploaded to be sent in one message. Unlike email protocols, HTTP supports the transmission of binary data, and the content of the file is transmitted ...


Only top voted, non community-wiki answers of a minimum length are eligible