question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi all,

I am attempting to use this directive to upload files into SharePoint, but it appears to be failing as the file needs to be Base64 encoded. How would I use this directive to encode via base64? My current code is as follows: (Note that the controller is bound at the page level)

HTML:

<div>
            watching model:
            <div class="button" ngf-select ng-model="files">Upload using model $watch</div>
            <div class="button" ngf-select ngf-change="upload($files)">Upload on file change</div>
            Image thumbnail: <img ngf-src="files[0]" ngf-default-src="'/thumb.jpg'" ngf-accept="'image/*'">
        </div>

Controller:

 appControllersUploadImage.controller('appUploadImageCtrl', ['$scope', '$location', 'Upload', function ($scope, $location, Upload) {

        $scope.$watch('files', function () {
        $scope.upload($scope.files);
    });

    $scope.upload = function (files) {
        if (files && files.length) {
            for (var i = 0; i < files.length; i++) {
                var file = files[i];
                Upload.upload({
                    url: "/sites/ens/_api/web/lists/getByTitle('Images')/RootFolder/Files/add(url='test.jpg',overwrite='true')",
                    fields: {'Title': "test"},
                    file: file,
                    headers: {
                        'Accept': 'application/json;odata=verbose', 'content-type': 'image/jpeg', 'X-RequestDigest': $("#__REQUESTDIGEST").val()
                    }
                }).progress(function (evt) {
                    var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                    console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
                }).success(function (data, status, headers, config) {
                    console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
                });
            }
        }
    };
}]);

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
arusakovcommented, Jun 25, 2015

@krusk use @danialfarid code and add something like this:

var fileReader = new FileReader();
fileReader.readAsDataURL(file);
fileReader.onload = function (e) {
  var dataUrl = e.target.result;
  var base64Data = dataUrl.substr(dataUrl.indexOf('base64,') + 'base64,'.length);
  $http.post('/url/to/upload/', { img: base64Data }).success(function () {
    // all right
  });
};
0reactions
amicoscommented, Jul 2, 2015

I used that:

                          var binary = atob(dataUrl.split(',')[1]);
                            var array = [];
                            for(var i = 0; i < binary.length; i++) {
                                array.push(binary.charCodeAt(i));
                            }
                           var leblob= new Blob([new Uint8Array(array)], {type: 'image/jpeg'});
                           leblob.lastModifiedDate = new Date();
                           leblob.name = "my-image.jpg";
Read more comments on GitHub >

github_iconTop Results From Across the Web

Base64 Encode and Decode - Online
Encode to Base64 format or decode from it with various advanced options. Our site has an easy to use online tool to convert...
Read more >
Base64 - Wikipedia
In computer programming, Base64 is a group of binary-to-text encoding schemes that represent binary data in sequences of 24 bits that can be...
Read more >
Base64 - MDN Web Docs Glossary: Definitions of ... - Mozilla
Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a ......
Read more >
What Is Base64 Encoding? - Level Up Coding - gitconnected
The base64 is a binary to a text encoding scheme that represents binary data in an ASCII string format. base64 is designed to...
Read more >
Base64 encoding: What sysadmins need to know - Red Hat
Base64 encoding uses printable characters to encode binary data. This enables another interesting use case: You can encode multiline files into ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found