dotnet-practitioner
dotnet-practitioner

Reputation: 14148

consolidate file choose and upload in one line in angularJs only

Currently, I choose the file and upload using 2 different buttons as follows:

<input type="file" file-model="myFile" />
<button ng-click="uploadFile()">upload me</button>

Is it possible to choose the file and upload in 1 single button?

I tried the following but did not work.

<input type="file" file-model="myFile" onchange="uploadFile()" />

Upvotes: 0

Views: 443

Answers (2)

Masum Billah
Masum Billah

Reputation: 2399

You can use ng-file-select like this:

<input type="file" ng-model="fname" ng-file-select="uploadFile($files)">

Inside your controller, you'll get object:

$scope.uploadFile = function($files) {
    var file = $files[0];
    console.log(file);
}

More details:
http://blog.gitbd.org/file-upload-by-angular-and-php/

Upvotes: 4

Venkatesh
Venkatesh

Reputation: 303

try this

<input type="file" file-model="myFile" onChange="uploadFile()" />

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script>

function uploadFile(){
     alert("function accessed");
}

</script>
</head>
<body>

<input type="file" file-model="myFile" onChange="uploadFile()" />

</body>
</html>

Upvotes: 0

Related Questions