function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sergio Mac-IntoshSergio Mac-Intosh 

How can i resize the size of my attachment in javascript.

Hi,
I got some functionality which uploads files from a visualforce page.
The problem is that mobile pictures are always to big. Because of this Salesforce won't accept the base64 because the size is to big.
How can i resize this before uploading. For small images the functionality works perfect.
Code atm:

Javascript:
function uploadFile()
{       
    var input = document.getElementById('file-input');
    var parentId = 'a0fL0000004TCDA';

    var filesToUpload = input.files;

    for(var i = 0, f; f = filesToUpload[i]; i++)
    {
        var reader = new FileReader();     

        // Keep a reference to the File in the FileReader so it can be accessed in callbacks
        reader.file = f; 

        reader.onerror = function(e) 
        {
            switch(e.target.error.code) 
            {
                case e.target.error.NOT_FOUND_ERR:
                    alert('File Not Found!');
                    break;
                case e.target.error.NOT_READABLE_ERR:
                    alert('File is not readable');
                    break;
                case e.target.error.ABORT_ERR:
                    break; // noop
                default:
                    alert('An error occurred reading this file.');
            };
        };     

        reader.onabort = function(e) 
        {
            alert('File read cancelled');
        };

        reader.onload = function(e) 
        {
            var att = new sforce.SObject("Attachment");
            att.Name = this.file.name;
            att.ContentType = this.file.type;
            att.ParentId = parentId;

            att.Body = (new sforce.Base64Binary(e.target.result)).toString();
            var testvar = att.Body;
            alert('Test:' + testvar.length );

            sforce.connection.create([att],
            {
                onSuccess : function(result, source) 
                {
                    if (result[0].getBoolean("success")) 
                    {
                        console.log("new attachment created with id " + result[0].id);
                    } 
                    else 
                    {
                        console.log("failed to create attachment " + result[0]);
                    }
                }, 
                onFailure : function(error, source) 
                {
                    console.log("An error has occurred " + error);
                }
            });
        };

        reader.readAsBinaryString(f);
    }
}
HTML:
<input id="file-input" type="file" name="file"/>
<input type="button" value="Add Review" style="font-size:12px;"  onclick="uploadFile;"/>

Thanks in Advance!

 
Hargobind_SinghHargobind_Singh
Hi, I am not aware of any javascript based code to resize your images. However, you should explore Heroku. It has an addon called bitline that you can use. You can create an app on Heroku, and then pass your image to that app to get resized. 

Alternatively, you can look for any online service that does that, but it may require a subscription.