0

I am working on an Angularjs application.

I am able to call the function when selecting a file, but it does not pass the selected file into the Angularjs function. I get an undefined value in the variable selectedFile.

I am using a trigger to open the file picker as follows:

$scope.clickUpload = function () {
    angular.element('#uploader').trigger('click');
}

The following is my HTML:

<button type="submit" ng-click="clickUpload()">
<span>Upload Image</span>
</button>
<input id="uploader" hidden type="file" ng-accept="'.jpg,.png,.gif,.jpeg,.webp'" ngf-select="uploadFile(selectedFile)" ng-model="selectedFile" name="file" />

Below is my Angularjs function to upload the file that has been selected:

$scope.uploadFile = function (selectedFile) {
    alert(selectedFile);
}

What is the mistake I have made? Thank you for reading my question.

1

1 Answer 1

-1

You can't use the ngModel directive in type="file" inputs. Do this instead:

$scope.clickUpload = function() {
    var fileInput = document.getElementById("uploader");
    fileInput.click();

    //do nothing if object doesn't contain a file
    if(fileInput.files.length === 0) return;

    var file = fileInput.files[0];
    alert(file.fileName);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I am getting only undefined passed to the var fileInput

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.