JS API createFileAsset Function for PDF


(Lewis) #1

Hi all,

Any idea why I’m getting this error from Matrix 5.5 when trying to use the JS API to create a PDF file asset:

error => The supplied file extension "" is not allowed for file type "pdf_file" errorCode => fileExtensionError

Here’s my code (straight from Squiz’s manual):

js_api.createFileAsset({
       "parentID":406731,
       "type_code":"pdf_file",
       "friendly_name":"File #1",
       "link_type":"SQ_LINK_TYPE_1",
       "link_value":"link value",
       "dataCallback":printObject
    }) 

There is no reference to an extension parameter in Squiz’s manual and I’ve even CTRL+F’d the page and can’t find any reference to ‘extension’!!

Also, does anyone have a clue what the ‘type_code’ is for a plain File asset?


Batch Request | JS API
(Nic Hubbard) #2

Only issue I can see is the docs say the parentID should be a string, not an integer.


(Lewis) #3

Thanks @nnhubbard for the reply!

Hmm, that’s even more interesting in which case because I’ve successfully used an integer with the createAsset function in the last few days.


(Lewis) #4

Ah, wait a minute - it is an integer for createAsset @nnhubbard, apologies. Hmm … weird. I’ll give it a go anyway. Thanks again for the reply!


(Lewis) #5

Just tried it out @nnhubbard and it doesn’t seem to make a difference - integer or string on createAsset, it works either way.

Just tried the createFileAsset function again and it still doesn’t work - quoting or not quoting the parent ID. I can also see that there’s no reference to createFileAsset in the Details screen of the Javascript API asset - the tick boxes that allow you to restrict what functions are available.


(Bart Banda) #6

What exact version are you on? I’d say this is probably a bug, can’t remember if it’s been fixed though.

Creating file assets with the JS API has always been tricky as Matrix usually requires an actual binary file to also be submitted in a file asset creation which is what happens in the backend UI.


(Lewis) #7

Morning @Bart. Thanks for the reply!

We’re running version 5.5.3.3.

From you description, it doesn’t seem like it’s something I’m missing in the code?


(John gill) #8

friendly_name is a badly named parameter - it’s actually filename in spirit so it needs to be an actual filename. That’s why it’s complaining about invalid blank extension. Make sure friendly_name is an actual .pdf filename and it works.


(Lewis) #9

Hi @JohnGill. Thanks for the reply!

Do you mean change friendly_name to filename on the left hand side (in the parameters)? I’ve done that now and, although it has moved on, it’s now producing the following error:

I’ve literally just copied and pasted the example code from the manual to try and get started - that’s what was in my original post.

I’ve tried adding ‘.pdf’ to the end of the filename too and get the same error (above).

I’m guessing this latest error means that there’s a step to undertake before you can use createFileAsset - actually uploading a file? This isn’t documented and ‘createFileAsset’ suggests it’s just that - a function to create the file in the first place. What has been your experience?

I’m guessing there’s a missing name parameter, based on the error above but again, nothing in the docs to suggest that’s needed.


(John gill) #10

Possibly a version difference, but the original example works for me on 5.5.1.5 as long as friendly_name is a valid PDF filename

console.log(await js_api.createFileAsset({
       "parentID":345,
       "type_code":"pdf_file",
       "friendly_name":"file.pdf",
       "link_type":"SQ_LINK_TYPE_1",
       "link_value":"link value"
    }) )

Object { 29128: "New File Asset (#29128) 'file.pdf' of type_code 'pdf_file' created successfully" }