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

how can I send IDs for each file? #687

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

how can I send IDs for each file? #687

salmanasiddiqui opened this issue Apr 22, 2015 · 4 comments

Comments

@salmanasiddiqui
Copy link

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
Copy link
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
Copy link
Author

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
Copy link
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 pushed a commit that referenced this issue Jun 4, 2015
@danialfarid
Copy link
Owner

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

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

2 participants