nervgh / angular-file-upload

[ALMOST NOT MAINTAINED] Angular File Upload is a module for the AngularJS framework
MIT License
3.43k stars 1.13k forks source link

formData not binding to scope? #399

Open uberspeck opened 9 years ago

uberspeck commented 9 years ago

On file upload I want to capture the file name and allow the user to update it before submitting (as a separate parameter (bookNumber) on formData). I'm using onAfterAddingFile() to capture the item._file.name and set the bookNumber. The $scope.bookNumber value is bound correctly and the FileUploader.formData also seems to be bound, but when I submit, bookNumber is always null. What am I missing?

angular

  .module( "app.book" )

  .controller( "NewBookCtrl", (FileUploader, api, ...) ->

    vm = @

    vm.record =
      bookNumber: null

    vm.uploader = new FileUploader(
      url: "/book/upload/path"
      alias: "bookFile"
      formData: [vm.record]
      queueLimit: 1
      onAfterAddingFile: (item) ->
        vm.record.bookNumber = item._file.name.replace(".pdf","")
      ...
    )

    return

  )

Screen shot of bindings all working as expected: screen shot 2015-04-25 at 3 27 45 pm

...and then, the request:

------WebKitFormBoundaryVn3eLM5cFtiwSFF8
Content-Disposition: form-data; name="bookNumber"

null // <= wtf?
------WebKitFormBoundaryVn3eLM5cFtiwSFF8
Content-Disposition: form-data; name="bookFile"; filename="2904 Photo Record CCC.pdf"
Content-Type: application/pdf

------WebKitFormBoundaryVn3eLM5cFtiwSFF8--
uberspeck commented 9 years ago

This seems to fix it for me...feels sloppy though :(

angular

  .module( "app.book" )

  .controller( "NewBookCtrl", (FileUploader, api, ...) ->

    vm = @

    vm.record =
      bookNumber: null

    vm.uploader = new FileUploader(
      url: "/book/upload/path"
      alias: "bookFile"
      formData: [vm.record]
      queueLimit: 1
      onAfterAddingFile: (item) ->
        vm.record = item.formData[0] = {bookNumber: item._file.name.replace(".pdf","")}
      ...
    )

    return

  )
unknownguy commented 9 years ago

Your fix would not work if you upload the file first and then change the form value.