Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

We have a requirement where we want to read a csv document in Jquery / Javascript.

We are able to read the file from documents (via query) and Static Resource inside the controller and then pass it to javascript.

code snippet - controller

PageReference pr = new PageReference('/resource/1358406667000/ppfBlobTest');
Blob content = pr.getContent();
String[] filecontent = content.toString().split('\n');
blogText = '<script>var st = '+ JSON.serialize(filecontent)+';  </script>';

code snippet - VF Page

<script>
  $j = jQuery.noConflict();
  $j(document).ready(function() {  
     alert(st);
  });
 </script>

But we want to access that file(document) directly from javascript...any solution how to access it?

Have anyone worked on loading a file directly in javascript (VF Page) ??

share|improve this question
add comment

4 Answers 4

Use a RemoteAction method in Apex and then you can just hand the stringized blob directly to jQuery.

share|improve this answer
add comment

You can use the SalesForce URLFOR function in combination with jQuery ajax support to retrieve the content of the static resource directly.

<script>
    $.get('{!URLFOR($Resource.ppfBlobTest)}', function(data) {

      // do something with the retrieved static resource data      
      alert(data);
    });

</script>
share|improve this answer
add comment

It sounds as if you are trying to bypass needing a controller (Apex) to access Chatter Files/Folders/Attachments. Static Resources are illustrated by the other posts. If so, you can query anything the SOAP API allows via the Salesforce Ajax toolkit, which is a Javascript wrapper for the SOAP API. You can use the Ajax toolkit in VF or in custom Javascript buttons for page layouts.

You could also just use the Salesforce REST or CHATTER APIs from Javascript using Ajax calls. In the case of records that include binary data fields, resultsets are limited to only one record anyway, so REST works just as well as SOAP for file type queries.

To use the Ajax Toolkit, put this include in your VF page:
<apex:includeScript value="/soap/ajax/26.0/connection.js"/>

Then you can query anything you like:
results = sforce.connection.query("select Body from Attachment LIMIT 1");

Then navigate the result, which will contain a Base64 encoded string:
blob = results.records.Body;

Then Base64 decode the string into the original file content (CSV in your question's case) and parse as desired.

Note: The user accessing the page may need to have the API permission enabled for the Ajax toolkit to work properly.

share|improve this answer
add comment

Hey You can use Javascript remoting to call a method in the controller, Pass the filename/URL/Id to the controller method and retrieve the corresponding file.Force.com cannot handle binary data hence you have to send the data back to js in base64 format. in js u have to convert the same into binary. But since you are dealing with csv, you can send the file body in string format only to the js.

For more details on apex remoting please go through the following link http://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting.htm

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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