4

I'm trying to attach files (images) to nodes via Services 3 from an external app using JavaScript.

From what I understand (and doing) is:

1) Create a node posting JSON to my endpoint domain.com/api/node.json

{
  "type":"foo",
  "title":"Hello World",
  "language":"und",
  "body":{"und":{"0":{"value":"Body Text"}}},
}

2) Create file posting JSON to my endpoint domain.com/api/file.json

{
  "filename":"image.jpg",
  "target_uri":"public://image.jpg",
  "filemime":"image/jpeg",
  "file":"myBase64String"
}

No problem at all at this point.

Then when trying to attach the files created to the node, is where I have trouble, not http errors, simply nothing happens.

First I have tried posting JSON to my endpoint domain.com/api/node/*nid/attach_file after successfully creating a new file, and getting back the fid

{
  "field_name":"foo",
  "filename":"image.jpg",
  "fid":"*fid"
}

Returning Status 200 OK empty text, but when viewing the node remains the same without the file attached.

I have also tried to create the files first and then when posting for node creation adding the fid, the node is created but no file attached.

{
  "type":"foo",
  "title":"Hello World",
  "language":"und",
  "body":{"und":{"0":{"value":"Body Text"}}},
  "field_foo":{"und":{"0":{"fid":"*fid"}}}
}

My field is a field type Image, widget Image. I have also tried in another field type file, with same results.

If someone can give me a heads up, what am I doing wrong or what is missing. I can't find any explanatory documentation on line.

Thank you.

2

2 Answers 2

0

try adding more data to the attached file (it might be that fid and filename is not enough I can't find the example I used a long time ago, which had a similar problem):

  • alt (might be required in general or only if marked as required in bundle)
  • title (might be required in general or only if marked as required in bundle)
  • uid
  • filemime
  • uri
  • status = 1
  • display = 1

Some references I could find quickly:

2
  • Thank you Andre-B. I have figured out that it actually works, the problem was (is) that the file field was set to accept up to 4 files, if I set it to accept only one file it does work. Any clue on how can I attach multiple files within the same file field?
    – Arturo
    Apr 10, 2015 at 6:35
  • "field_foo":{"und":{"0":{"fid":"*fid"}}} did you try to use a real array here? "field_foo":{"und":{[{"fid":"*fid"}, {"fid":"*fid"}]}}? maybe search for multivalue fields and services Apr 10, 2015 at 7:50
0

It looks like this is related to the issue I raised on the Services module issue queue File is created but can't view it in the browser.

I tried a few times to save a file by attaching the base64 encoded data, returned by Javascript whenever I load a file from my application.

I could only make it work by removing the base64 identifier at the beginning of the string which seems too much of a hack... So I modified Services' file resource, using file_get_contents() in order to retrieve the image content.

You can find a patch on the issue page. Word of caution, although , I simply replace a function, this patch hasn't yet been tested!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.