2

Is it possible to set an encoding for a file that is uploaded via a VisualForce page? I am getting errors stating that the content of the uploaded file is not UTF-8 encoded. Ideally, the user would be able to specify the file encoding himself, similar to the Account and Contact import wizard.

  • You want to upload your files in static resource ?? Please provide more information !! – Javascript Coder Jun 3 '13 at 7:54
  • I just want to post a CSV file via a form and process its contents, not saving it in salesforce. – Patrick Oscity Jun 3 '13 at 9:27
2

Assuming your CSV file is being processed by a VF controller as part of a binding from the apex:inputFile component to a controller variable of type Blob, then, if you want to manipulate the CSV contents in the controller, the Blob must be converted to a String using Blob instance method myBlob.toString().

This toString() method requires that the Blob contain only UTF-8 characters as all Apex string operations are done in UTF-8.

Conclusion, the uploaded file when presented to the VF page has to be in UTF-8 before the page action executes.

|improve this answer|||||
  • Thanks, that answers the question. Our "fix" is to extend our documentation so that users know how to produce valid UTF-8 documents. Salesforce drives me crazy sometimes… – Patrick Oscity Jan 14 '14 at 10:57
  • Be sure to let your users know that MSFT Excel will not save CSV files in UTF-8 unless they start in UTF-8. You need to use Google docs, Open Office, or convert the file using Notepad or other text editor. If all the chars are ASCII A-Z, a-z, 0-9 and common punctuation, then ASCII = UTF-8 – cropredy Jan 14 '14 at 20:28

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.