Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample code: $watch firing upon page load #525

Closed
clabough opened this issue Jan 27, 2015 · 5 comments
Closed

Sample code: $watch firing upon page load #525

clabough opened this issue Jan 27, 2015 · 5 comments

Comments

@clabough
Copy link

Using the sample code, the upload event is firing twice upon loading of the page. I had to change it to this to prevent that.

    $scope.$watch('files', function() {
    if ($scope.files == null) return;  //<- Added this line
    $scope.upload = $upload.upload({
  url: 'server/upload/url',
  data: {myObj: $scope.myModelObj},
  file: $scope.files
}).progress(function(evt) {
  console.log('progress: ' + parseInt(100.0 * evt.loaded / evt.total) + '% file :'+ evt.config.file.name);
}).success(function(data, status, headers, config) {
  console.log('file ' + config.file.name + 'is uploaded successfully. Response: ' + data);
});
    }

Also, evt.config.file.name and config.file.name are undefined. Using the latest Chrome and .NET Web API backend. Note: I determined that using evt.config.file[0].name worked.

@Ilkorn
Copy link

Ilkorn commented Feb 3, 2015

Please comment this becouse it's truely important issue.

@danialfarid
Copy link
Owner

I updated the readme to have evt.config.file[0].name instead of evt.config.file.name which was wrong.

The change event is firing twice because when you click on the button it set's the value to null to handle the case when user cancels the popup (there is no easy way to detect this event in browsers other than Chrome which by default sets the value to null) and selecting the same file again.
So you are doing it right if the files are empty you don't need to upload anything but that is a correct event since the user might cancel the selected file and that's why you get that model change.

@danialfarid
Copy link
Owner

Actually the problem with file[0] was the way the file was passed in to the upload, it should've been $scope.files[0] I have updated the readme.

@jordancrawfordnz
Copy link

It says file.lenght in the sample JavaScript.

Also, I needed $scope.files = [];

@danialfarid
Copy link
Owner

I have updated the sample and included a jsfiddle to show how it works in version 3.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants