3

I'm working on a visualforce page to give our portal users ability to upload Content Documents to Library. I'm using the solution in this article: http://www.allsalesforce.com/articles/share/756339/. I uploaded the forcetk.js as a static resource (Source: https://github.com/developerforce/Force.com-JavaScript-REST-Toolkit/blob/master/forcetk.js) and added a new remote site setting with our sandbox URL.

Before using this javascript to invoke Force.com REST API, I used a visualforce page with <apex:inputFile value="{!file.versionData}" fileName="{!file.pathOnClient}" />. It worked fine for files < 10 MB. But the files from our portal users would be larger than that. So I'm trying to use REST API to increase the file size limit to 2 GB.

The code I added to our sandbox is:

<apex:page standardController="Case">
    <p>
        Select a file to upload.
    </p>
    <input type="file" id="file" onchange="upload()"/>
    <p id="message"></p>
    <script src="{!$Resource.forcetk}"></script>
    <script>
        var client = new forcetk.Client();
        var result = sforce.connection.login("adminusername", "password"+"securitytoken"); 
        client.setSessionToken(result.sessionId);
        function upload() 
        {
            var file = document.getElementById("file").files[0];
            client.createBlob('ContentVersion', {
                Origin: 'C', 
                PathOnClient: file.name
            }, file.name, 'VersionData', file, function(response){
                console.log(response);
                document.getElementById("message").innerHTML = "File uploaded: <a href=\"/" + response.id +
                    "\">Take a look!</a>";
            }, function(request, status, response){
                console.log(response);
                document.getElementById("message").innerHTML = "Error: " + status;
            });
        }
    </script>
</apex:page>

I added a new custom link on Case object with the above vf page as the source. When I try to upload a file using this link on Case record, I'm getting this error: Error: Unable to forward request due to: no protocol: null/services/data/null/sobjects/ContentVersion/. I also tried with Origin: 'H' (chatter file), but got the same error. I have no clue why I'm getting this error. Any help is appreciated. Thanks!

1
  • Were you able to figure this out anyways? Ability to upload file using REST API via Sites Page?
    – VarunC
    Commented Jan 12, 2021 at 21:41

1 Answer 1

3

I see you are using Viusalforce and I feel you are messing with Session ID .I tested below code and works in my org

<apex:page standardController="Case">
<p>
    Select a file to upload.
</p>
<input type="file" id="file" onchange="upload()"/>
<p id="message"></p>
<script src="{!$Resource.forcetk}"></script>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>

var client = new forcetk.Client();
client.setSessionToken('{!$Api.Session_ID}');

    function upload() 
    {
        var file = document.getElementById("file").files[0];
        client.createBlob('ContentVersion', {
            Origin: 'C', 
            PathOnClient: file.name
        }, file.name, 'VersionData', file, function(response){
            console.log(response);
            document.getElementById("message").innerHTML = "File uploaded: <a href=\"/" + response.id +
                "\">Take a look!</a>";
        }, function(request, status, response){
            console.log(response);
            document.getElementById("message").innerHTML = "Error: " + status;
        });
    }
</script>

Two things I did differently

  1. The Session Id can be easily obtained using global merge field {!$Api.Session_ID}
  2. I included jquery library as the whole set up needs jquery library to function properly
10
  • Hi Mohith, I'm using an admin's username and password to get the sessionid because the end user using this vf page will be a portal user and he/she wouldn't have permission to upload a content document. So I'm unable to use {$Api.Session_ID} as it would get the session id of the logged in portal user. I didn't mention in my original post but I did use "<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>", but no luck. If I'm doing something wrong with how I'm sending the username, password, and security token then can you please help with fixing that? I appreciate your help. Thanks!
    – user23972
    Commented Aug 17, 2015 at 17:05
  • Hi Mohit, Can I use this in lightning controller ? and how to link document with a record using this ? Commented Jan 23, 2017 at 11:01
  • @max you cannot use this in Lightning controller since the callout from Lightning controller is not allowed yet Commented Jan 23, 2017 at 11:02
  • Mohit, can you please take a look at the below link. I am really struggling with uploading files in contentVersion using lightning component. salesforce.stackexchange.com/questions/157024/… Commented Jan 23, 2017 at 11:05
  • I want to know if we can upload a file of size more that 4.3 MB in lightning component or not ? Any help or alternative solution will be greatful Commented Jan 23, 2017 at 11:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .