how can I send IDs for each file? #687

Closed
salmanasiddiqui opened this Issue Apr 22, 2015 · 4 comments

Projects

None yet

2 participants

@salmanasiddiqui

Im using 3.3.4 ng-file-upload

I am using Rails 4.2 as my server side app, I am sending some form data with multiple files on server. Following is my json structure

{
    data: "json.stringified form data",
    attachments_attributes: [
        {document: "file1"},
        {document: "file2"}
    ]
}

I achieved this by calling $upload.upload as following

$upload.upload({
    url: globals.API_URL + '/tickets/' + $routeParams.id,
    method: 'PUT',
    data: vm.data,
    file: vm.files,
    fileFormDataName: 'attachments_attributes[][document]'
})

vm.files is binded to the ng-file-upload element, vm.data is a hash of all the form fields.
Now, what I want is to add id key to attachments_attributes hash,

{
    data: "json.stringified form data",
    attachments_attributes: [
        {id: 'id_file1', document: "file1"},
        {id: 'id_file2', document: "file2"}
    ]
}

is it possible ?

@danialfarid
Owner

Why don't you put in the the data?
Also you don't need to stringify the data since it will be send as application/json format so server might be able to receive it as the actual json object.
You can also user

fields: {attachments_attributes: [
        {id: 'id_file1', document: "file1"},
        {id: 'id_file2', document: "file2"}
    ]}
@salmanasiddiqui

I am using data to submit form values.. and ng-file-upload is sending the data as stringified. I am not stringifying it anywhere.

If I put

{  
  attachments_attributes: [
      {id: 'id_file1'},
      {id: 'id_file2'}
  ]
}

the above in data then I will receive the above in data key on server. Which is not what I require, I want files and their ids in one array of hashes.

You also pointed to use fields, I thought ng-file-upload only handle files when passed to file param?

@danialfarid
Owner

Yea the files can only be send using file param, and they are separate from other data you are sending. You can set the fileFormDataName='file[0], file[1], ...' for them to be received as an array on the server and send the ids in anther array with data or fields.

@danialfarid danialfarid pushed a commit that referenced this issue Jun 4, 2015
Danial Farid Fixed #783 #764 #777 #766 #763 #761 #721 #687 #650 #710 #784 #768 #789 39fa784
@danialfarid
Owner

Since version 5.0.0 you can set sendFieldsAs:'form' which would do this for you.
See the PHP wiki page for samples

@danialfarid danialfarid closed this Jun 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment