Jump to content

File upload (images, pdfs) from base64 value


Raymond Geerts
 Share

Recommended Posts

I'm working on a XML RPC api to communicate between WoodWing Enterprise and a ProcessWire site.

One of the method calls i'm building is pw.uploadPWFile which has to upload files, it does this by posting a XML RPC call to this method sending the following parameters as payload:

  • Page ID
  • Username
  • Password
  • Data
    • Filename
    • MIME Type
    • Bits (base64)

I could not find any example on how to upload a file from base64 binary posted data, and how to assign this file to a field.

Are there any examples available or can sombody point me in the right direction?

Link to comment
Share on other sites

@LostKobrakai thanks for the tip. Martijn also mentioned this method.

To be sure the temp file gets removed properly after a certain amount of time, i will use the WireTempDir class to create a tmp folder which will be removed automaticly after a X amount of time.

Create the file (from base64) in there with fwrite and assign the file path of the tmp file to my image field.

  • Like 1
Link to comment
Share on other sites

$tmpDir = new WireTempDir('someName');
// add files
$fileBasename = 'basename.ext';
$filename = $tmpDir->get() . $fileBasename;
file_put_contents($filename, base64_decode($b64Data));
$page->files->add($filename);
// repeat for all files ...

// optionally, at the end call
$tmpDir->removeAll();

EDIT: Ah, you already know / found WireTempdir() :)

EDIT2: If one use WireTempDir for very timeconsuming tasks, one optionally can define a maxAge for it, different than the default 120 seconds:

$tmpDir = new WireTempDir('someName', array('maxAge' => 300));
  • Like 8
Link to comment
Share on other sites

  • 1 month later...

ok maybe thats related to tracydebugger?

ErrorException: Class 'wireTempDir' not found in /var/www/...module:65
Stack trace:
#0 [internal function]: Tracy\Debugger::shutdownHandler()
#1 {main}
(stored in /var/www/.../site/assets/logs/tracy/exception--2016-03-02--17-39--0979d2c325.html)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...