vendredi 30 octobre 2015

Ember js - file upload component - force change event to fire on during second attempt to upload the same file

I am using the Ember Uploader component to allow users to upload a .txt file.

This file has to be formatted in a very specific way, and the upload cannot be allowed if the file is not formatted correctly.

The component first reacts to the change event, which watches for a new file being selected.

This sets the property 'files' to the selected file, which causes the function filesDidChange to run.

In filesDidChange, the file is read and checked for formatting errors. If it finds errors, it tells the user to update the file and try again.

Everything works perfectly on the first attempt. The problem is that when you try to re-upload the edited file, the component does not recognise this as a change event- the alert("Hello World") doesn't fire.

I assume this is because the same file has been selected.

I've tried setting 'files' to null at the end of the filesDidChange function, but that has no effect. This makes sense, as the change event watches the element, rather than a property.

How can I get the change event to fire, even if the same file is selected to upload on the second attempt?

change: function(e) {
    alert("Hello World");
    var input = e.target;
    var selectedFiles = input.files;
    if (selectedFiles) {
      this.set('files', selectedFiles);
    }
  },
filesDidChange: function(files) {
    //Reads the text file using the FileReader API.
    //Checks that the file is correcty formatted.
    //Prevents the upload and displays an error message if the file format is wrong.
    //Asks the user to fix the errors and try to upload the same file again.
}.observes('files'),



Aucun commentaire:

Enregistrer un commentaire