Read uploaded file contents

Hello Experts!

How do you read file contents of the file selected using frappe.ui.FileUploader

Thanks

Hi, @enxnil

for this case you can use the “FileReader” API in js.

frappe.ui.form.on("YourDocType", {
    your_file_field: function(frm) {
        var file_upload = frm.fields_dict.your_file_field;
        var file = file_upload.get_value();

        if (file) {
            var file_reader = new FileReader();

            file_reader.onload = function() {
                var file_contents = file_reader.result;
                // do something with the file contents
            };

            file_reader.readAsText(file);
        }
    }
});

The your_file_field is the fieldname of the frappe.ui.FileUploader field. while selecting the and uploading it the onload function will called the object FileReader .
The above example read the file text using the method called readAsText.

some other method are given below for this you need to just replace the methods in readAsText

  1. readAsArrayBuffer(file): Reads the contents of the file as an ArrayBuffer object.
  2. readAsBinaryString(file): Reads the contents of the file as a binary string.
  3. readAsDataURL(file): Reads the contents of the file as a data URL (base64-encoded string).
  4. readAsText(file, encoding): Reads the contents of the file as a text string. The optional encoding parameter can be used to specify the character encoding of the file (default is UTF-8).

Thank you.

1 Like

@VINOTH Thanks a lot for your time and help

That saved me days!

@enxnil With delight :dizzy:.