Comments (5)
Yes. I upload files via API. POST Content-Type: multipart/form-data; boundary=X-INSOMNIA-BOUNDARY
But if you need to download large files then you need to set AllowAjax variable
$ul = wire(new WireUpload($formName));
$ul->setValidExtensions(['mp4', 'avi', '3gp']);
$ul->setMaxFiles(1);
$ul->setMaxFileSize(100 * 1000000); // 100 MB
$ul->setOverwrite(true);
$ul->setDestinationPath($p_path);
$ul->setLowercase(true);
$ul->setAllowAjax(true);
$files = $ul->execute();
from restapi.
As example add line in Routes.php
['POST', 'sp/{id:\d+}', Example::class, 'setPage', ["auth" => false]],
It give you POST request by address: Your_Domain/sp/page_id (as ex. localhost/sp/1821)
Next step edit Example.php and add function.
We will send different data:
id from routing (int);
field_digit (int);
field_name (string);
field_double (float);
photo (file).
public static function setPage($data) {
// Sanitize your fields to use
// but we don’t have a file here. we will take it the other way.
$data = RestApiHelper::checkAndSanitizeRequiredParameters($data, ['id|int'],['field_digit|int'],['field_name|string'],['field_double|float']);
// give a response
$response = new \StdClass();
// get page by id
$page = wire('pages')->get($data->id);
if(!isset($page->id)) throw new \Exception('page not set');
// change fields in page
$page->title = $data->field_name;
$page->number = $data->field_digit;
$page->temperature = $data->field_double;
$p->save();
Now let's talk about the file
// Use a global variable
if($_FILES){
// PHP load errors with code
$f_error = $_FILES["up_photo"]["error"];
// request header
$fheader = getallheaders();
$f_size = $fheader["Content-Length"];
// name label by reguest
$formlabel = 'up_photo';
// name of file by request
$formName = array_key_first($_FILES);
$TempDir = $_FILES[$formName]['tmp_name'];
// path where this file is usually located in PW
$p_path = wire('config')->paths->assets . "files/". $page->id . "/";
// next file manipulating
$ul = wire(new WireUpload($formName));
$ul->setValidExtensions(['jpg', 'jpeg', 'png']);
$ul->setMaxFiles(1);
$ul->setMaxFileSize(100 * 1000000); // 100MB
$ul->setOverwrite(true);
$ul->setDestinationPath($p_path);
$ul->setExtractArchives(false);
$ul->setLowercase(true);
$ul->setAllowAjax(true);
$files = $ul->execute();
$err_array = $ul->getErrors();
// WireUpload errors
if($ul->getErrors()) throw new \Exception("ErrUp:".$err_array[0]);
if(count($files)) {
// take the first file
$file = $p_path . reset($files);
// do something with your file
$resp = $page->photo->add($file);
if($page->photo->$resp->filesize == 0 ) throw new \Exception('Photo not correctly upload');
$page->save();
$response = $files[0];
}
Sorry if I wrote with errors. I just wanted to state the principles.
from restapi.
Thanks a lot @andy-scboy !!! This helps me a lot! I would have never figured this out myself. Thanks, thanks, thanks!!!
from restapi.
@andy-scboy Thanks for stepping in, very much appreciated! :)
@spoetnik Generally speaking the module does not limit what you can do, basically it's just a router to your own functions, so anything you can do in PHP or in ProcessWire should be possible with this module aswell.
from restapi.
Thanks for the help @andy-scboy but can you please help me a bit further?
Looking at some examples, the use the $data variable to access the POST data.
Line 46 in b628d6b
My 'files'-field is not in the $data-array my function receives.
You mention in your code-example above '$formName', that's not part of the $data-array.
Can you please give me a code example of the function receiving the POST data?
Thanks in advance!!
from restapi.
Related Issues (9)
- Running PW in a sub-folder HOT 7
- Snippet HOT 2
- Question on Code snippet HOT 3
- Just a Thank You HOT 2
- Status codes HOT 1
- Error: Trying to access array offset on value of type null :: Router.php:286 HOT 4
- json pretty print for superuser HOT 3
- Hookes not fired HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from restapi.