Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Web services files handling: Difference between revisions

From MoodleDocs
Line 13: Line 13:


The entry point is /webservice/upload.php, simply use HTTP POST method to upload files, it requires a web service token for authentication. If the upload is successfully, the files will be saved in the user private file area. The files information will be returned in JSON format. If an error occurs, an error message will be sent back in JSON format too.
The entry point is /webservice/upload.php, simply use HTTP POST method to upload files, it requires a web service token for authentication. If the upload is successfully, the files will be saved in the user private file area. The files information will be returned in JSON format. If an error occurs, an error message will be sent back in JSON format too.
Some webservice methods can accept files, the way to call those functions is to use the above upload script with an optional url parameter: filearea=draft. This will add the file to a new user draft area and return the draft id. To add multiple files to a draft area, call it again and pass another url parameter draftid=? which is the draft id returned from the first upload request. Once all the files are uploaded, you can call the webservice that accepts files and pass it the draftid of the draft area containing the list of files for the request. An example of a webservice that accepts files is: mod_assign_save_submission.


Look at the [https://github.com/moodlehq/sample-ws-clients/tree/master/PHP-HTTP-filehandling code example on Github].
Look at the [https://github.com/moodlehq/sample-ws-clients/tree/master/PHP-HTTP-filehandling code example on Github].

Revision as of 06:58, 4 November 2014

Summary

Since Moodle 2.0, we provide web service functions to upload and download files. They are:

  1. moodle_file_get_files (Deprecated, use core_files_get_files since moodle 2.2 onward)
  2. moodle_file_upload (Deprecated, use core_files_upload since moodle 2.2 onward)

File contents are encoded in base64, and for web service transmission, it's not efficient. Mobile devices don't have enough memory to decode/encode web service request/response containing large files.

So we developed some alternative solutions to upload/download files.

File upload

The entry point is /webservice/upload.php, simply use HTTP POST method to upload files, it requires a web service token for authentication. If the upload is successfully, the files will be saved in the user private file area. The files information will be returned in JSON format. If an error occurs, an error message will be sent back in JSON format too.

Some webservice methods can accept files, the way to call those functions is to use the above upload script with an optional url parameter: filearea=draft. This will add the file to a new user draft area and return the draft id. To add multiple files to a draft area, call it again and pass another url parameter draftid=? which is the draft id returned from the first upload request. Once all the files are uploaded, you can call the webservice that accepts files and pass it the draftid of the draft area containing the list of files for the request. An example of a webservice that accepts files is: mod_assign_save_submission.

Look at the code example on Github.

File download

We serve the files through /webservice/pluginfile.php. This script requires a web service token for authentication.

Look at the code example on Github.

In case of issue, think to check that:

  1. the service associated with the token allow "files download" (Administration > Plugins > Web services > Manage services > Edit service > Advanced button)
  2. the web service is valid

Note: you could notice that /webservice/pluginfile.php has the exact same stucture than /pluginfile.php. We don't serve the files through /pluginfile.php for web service clients because this script requires user's login session to work. It's why it might not be an option for web service client.

See also