Note that there are some explanatory texts on larger screens.

plurals
  1. POPython REST decoding the results of FileReader::readAsDataURL
    primarykey
    data
    text
    <p>I am using Python / Django to process a REST request - one of the elements in the REST is the Data URI of a file to upload. It looks like everything is fine (js captures the file and builds the json correctly), but when I decode the file on the server and write the binary to disk it is different.</p> <p>I am new to python/django from a .NET and Java background - any help is more than appreciated. Thank you</p> <p>HTML input control</p> <pre><code>&lt;input type="file" class="col-lg-6" style="padding-left: 0px" onchange="angular.element(this).scope().attachment = this.files[0]; angular.element(this).scope().$digest();"/&gt; </code></pre> <p>Add Image from my angular JS controller</p> <pre><code>$scope.addImage = function () { var attachment = $scope.attachment fr = new FileReader(); fr.onload = function () { $scope.public_file = $scope.public_files.concat({filename: attachment.name, data: fr.result}) $scope.$digest() }; fr.readAsDataURL(attachment); } </code></pre> <p>Parsing on the server</p> <pre><code>def getData(uri): head, data = uri.split(',') decoded =data.decode('base64','strict'); # decoded base64.b64decode(data) return decoded def create(self, request): data = getData(request['public_files']) with open(file_path, 'w+') as f: f.write(data) return super(UpdatingIdeasViewSet, self).create(request) </code></pre> <hr> <p>Comparing console.log(fr.result) on the client with request['public_files'] on the server yields the same string. It must be a base64 decoding issue</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload