Hi Everyone,
Thanks to the help of this group I have been able to get all the retrieval and listing functions working - and now I hope to do the reverse.
I read here that the REST server can accept a file posted to it (http://groups.drupal.org/node/90819#comment-285609), however I am struggling to understand how to make this work using JQuery. I've found the fileRecieve function in the RESTServer.inc but cannot determine how to post a file there or what variables I should send with it.
Right now I am just trying to get the below working with a standard form (then I'll introduce the fun of iframes etc to do this asynchronously).
Right now I get a 406 with the above - does this mean I should be using a different action? Or am I missing some variables from the input form.
I am trying to work out how this function is connected to the /endpoint/file create (or POST) action - any suggestions here would be greatly appreciated.
Is this the best approach or are there some external file upload options using alternative modules I should consider?
Thanks in advanced for your expertise and assistance.
Regards,
GJ
Comments
Just guessing from reading
Just guessing from reading the logic in file_resource.inc, a file create looks like it requires the POST data to contain a slightly non-standard File object with this structure:
fid: database file id, only supply this field if you're updating an existing database record, for new file creates this field should not exist
uid: user id of file owner
filename: should be real filename, with extension; extension used in validation logic
filepath: location in server's local filesystem where file will be saved
filemime: mime type of file data (I think get gets set by the logic and may not be required)
filesize: file data byte count
status: set to FILE_STATUS_PERMANENT for permanent storage status or FILE_STATUS_TEMPORARY if this is only for temp use and will be auto deleted next cron run
timestamp: current time() value
file: ACTUAL FILE DATA, base64 encoded, non-standard field in File object; this field only exists for this Services routine's transport of the file data.
Interesting in that it also looks like there is no Update operation... Comments say that a file update should be handled via a new file create followed by an old file delete. However, in the code it supports an "update" operation via the presence of the 'fid' field triggering 'update file handling' logic.
So, it looks like something like this should handle the jQuery POST:
data = {
uid: integer,
filename: 'string-with-valid.ext',
filepath: 'server/local/path/to/save/file',
filesize: integer, // the non-base64 encoded byte size, normal byte size
timestamp: integer, // = time()
file: string, // file data base64 encoded
};
$.ajax({ type: 'POST',
url: api-endpoint-path/resource,
data: JSON.stringify({data: data}), // <- notice extra wrapper around structure, apparently required (I suggest you try with and without extra wrapper )
dataType: 'json',
contentType: 'application/json',
success: it_worked_callback,
error: it_failed_callback
});