Skip to content

uploadAll sending stale formData #225

Closed

Description

@PawelDecowski

I have a form which, in addition to uploading a file, sends some other fields. For clarity, I send only one field in this example:

$scope.save_with_upload = function(user) {
    $scope.uploader.formData = [{
        name: user.name,
    }];

    $scope.uploader.uploadAll();
}

Let’s say the value of the name input is Test 1.

  1. When I first load the page and click Save, it sends the file but doesn’t send formData at all.
  2. Then I change name field to Test 2 and hit Save. It sends the file and formData, but the value of name it sends is Test 1.
  3. Then I change name field to Test 3 and hit Save. It sends the file and formData, but the value of name it sends is Test 2.
  4. … and so on …

So it seems to always send the data that was assigned to formData before the last call to uploadAll.

To clarify: If I do dir(user, $scope.uploader.formData) just before calling uploadAll, it shows correct up-to-date values in both.

I’ve been struggling with it for a few hours and just can’t seem to see what’s wrong. Any ideas?

Activity

  1. sraparla commented on Aug 27, 2014

    @sraparla

    if you want to send the form data along with the upload..do it this way
    var uploader = $scope.uploader = new FileUploader({
    url:'someUrl',
    formData:[]
    })
    uploader.onBeforeUploadItem = function(item) {
    item.formData.push({kf_Upload: $scope.dynamicUploadID['kf_Upload']});
    }

  2. PawelDecowski commented on Aug 27, 2014

    @PawelDecowski
    Author

    @sraparla I can’t do it this way because I don’t know the values of the fields at the time of instantiation of the FileUploader class. The form fields can be edited by the user after the class has been instantiated so I need to be able to assign the formData after the user has hit Save and before calling uploadAll.

  3. sraparla commented on Aug 27, 2014

    @sraparla

    yeah..i don't understand..can u provide a jsfiddle of what you want

  4. nervgh commented on Aug 29, 2014

    @nervgh
    Owner

    @PawelDecowski , see faq How to update FileItem options before uploading

    // add all fields
    uploader.onBeforeUploadItem = function(item) {
        Array.prototype.push.apply(item.formData, uploader.formData);
    };
  5. PawelDecowski commented on Aug 29, 2014

    @PawelDecowski
    Author

    @nervgh Thanks, that works. But what fixed it? Because you’ve changed a couple of things. Is it the use of the callback, or the fact that you attach the data to a FileItem, rather than to the FileUploader?

  6. nervgh commented on Aug 29, 2014

    @nervgh
    Owner

    But what fixed it?

    Nothing. FileItem will be created when the file will be added to queue. In this moment uploader options will be copied to FileItem

  7. stephaneeybert commented on Sep 29, 2014

    @stephaneeybert

    I'm still puzzled...

    First I wonder what is the difference between the options attribute and the uploader.formData property.

    Second, I also was pushing the uploaded formData onto the file item formData with:

    uploader.onBeforeUploadItem = function(item) {
      Array.prototype.push.apply(item.formData, $scope.uploader.formData);
      console.info('onBeforeUploadItem', item);
    };
    

    But doing this would duplicate the file item formData item object and have a formData array of length 2 instead of 1 element.

    Here is how I set my uploader.formData property:

    $scope.uploader.formData = [{
      rolloutId: currentRollout.id
    }];
    

    So I removed the call: Array.prototype.push.apply(item.formData, $scope.uploader.formData);

    Now I only do the:

    $scope.uploader.formData = [{
      rolloutId: currentRollout.id
    }];
    

    And it works fine.

    But I sense I don't fully understand the thing...

  8. rboughani commented on Oct 10, 2014

    @rboughani

    Thanks it work

  9. mani242530 commented on Nov 4, 2014

    @mani242530

    HI.

    how do I pass formdata. plz advsie. I was trying for long hours.
    I need to pass these three datas along with the file.
    accountList: $scope.accountList.Value,
    filedescriptor: $scope.filedescriptor,
    txntypename: $scope.txntypename

    var uploader = $scope.uploader = new FileUploader({
    url: 'en/external/bmls/batches/uploadbatch.bml',
    formData:[]
    });

    please reply asap. many thanks

  10. michaelmccarver commented on Apr 7, 2015

    @michaelmccarver
    $scope.uploader = new FileUploader({
        url: $scope.uploadUrl,
        removeAfterUpload: true,
        onBeforeUploadItem: function (item) {
            var stuff = {
                profiles: [1426, 1427, 1428],
                profiles_groups: '',
                upload_type: 'BROWSER',
                username: $scope.username,
                file_name: $scope.uploader.queue[0].file.name,
                file_size: $scope.uploader.queue[0].file.size,
                file_type: $scope.uploader.queue[0].file.type,
                file_created_date: 135479928,
            }
            item.formData.push(stuff);
        },
        onSuccessItem: function (item, response, status, headers) {
    
        },
        queueLimit: 1,
    });
    

    OR
    onBeforeUploadItem: function (item) {
    var stuff = {
    profiles: [1426, 1427, 1428],
    profiles_groups: '',
    upload_type: 'BROWSER',
    username: $scope.username,
    file_name: item.file.name,
    file_size: item.file.size,
    file_type: item.file.type,
    file_created_date: 135479928,
    }
    item.formData.push(stuff);
    },

    My biggest misconception was that the form data is attached to the item and not the uploader.
    @nervgh Great tool, thanks for your hard work...

  11. yasharma commented on Jul 31, 2016

    @yasharma

    one can also do like that, if someone wants to send data on click of button with attached function save()

    $scope.uploader.onBeforeUploadItem = function(item) {
        item.formData.push($scope.post);
    };
    
    $scope.save = function(user) {
        $scope.uploader.uploadAll();
    };
    

    $scope.post will contains your data eg. title, body.

  12. sebastianfelipe commented on Oct 17, 2016

    @sebastianfelipe

    On the server side, how can you get the data in that formData? I can't find it, I tried in req.body, req.query, req.params, ctx.args (loopback nodejs). Thanks.

  13. yasharma commented on Oct 18, 2016

    @yasharma

    @sebastianfelipe you can get your uploaded file on server side within req.file and data would be in req.body.

  14. sebastianfelipe commented on Oct 18, 2016

    @sebastianfelipe

    mmm... I can't receive anything in those variables, I'm using loopback, and in the beforeRemote method it still without appear. In the afterRemote method I can get access at the file variable, but not before. I would like to use the req.body, that is the only but it appears after the whole file has been uploaded, and is late to do some staff, like reject the file :c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions