82
loading...
This website collects cookies to deliver better user experience
node--{entity_type}
. Think of Nodes as your Schema, the actual content your site will be built around, and main thing that is returned form database each time you make a request. user--user
. file--file
.POST /jsonapi/node/article/{uuid}/field_image HTTP/1.1
Content-Type: application/octet-stream
Accept: application/vnd.api+json
Content-Disposition: file; filename="filename.jpg"
[… binary file data …]
/** Step 1 Upload to field
POST /jsonapi/node/article/field_image HTTP/1.1
Content-Type: application/octet-stream
Accept: application/vnd.api+json
Content-Disposition: file; filename="filename.jpg"
[… binary file data …]
/** RES will contain picture {picture_uuid}
/** Step 2 - PATCH uuid to reference entity within your node or user resource
PATCH /jsonapi/node/article/{uuid}/relationships/field_image HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": {
"type": "file--file",
"id": "{UUID of new file entity}",
"meta": {
"description": "The most fascinating image ever!"
}
}
}
[Binary File data]
. Binary File data is an array, that chops up your image and sends it in slices to your server. The way we will send our slices of data, is through a JS method called an ArrayBuffer. Using the FileReader Module, we can slice our image into a stream and send it on its way in an Array to our backend. import React from 'react'
export const FileUploader = () =>{
const [fileUpload, setFileUpload] = useState(null);
const [name, setName] = useState('');
const handleChange = (e) => {
const acceptedTypes = ['image/png','image/jpg']
let file = e.target.files[0]
if (acceptedTypes.indexOf(file.type) == -1){
alert("improper file type: must be .png or .jpg")
} else {
// keep track of file in state
setFileUpload(file)
setName(file.name)
}
}
const handleSubmit = (e) => {
e.preventDefault()
}
return(
<form onSubmit={handleSubmit}>
<input
type='file'
onChange={handleChange}
id='myFile'
name='myFile'/>
<input type="submit"/>
</form>
);
}
handleSubmit()
function to send our file properly. Since our header Content-Type will be set to application/octet-stream
, we cannot use form-data to send our file. Instead we will send it as an Array Buffer.//inside of the handleSubmit function
let myFile = fileUpload // from react state
let myFileName = name // file name from react state
//example of uri for axios request
// example header object
const headers = {
"Content-Type": "application/octet-stream",
"Accept": "application/vnd.api+json",
"Content-Disposition": `file; filename="${myFileName}"`,
"X-Crsf-Token" : `${session_token}`,
"Authorization":`Bearer ${token}`
}
// File Reader Class
let reader = new FileReader();
//Convert our file into Array Buffer
reader.readAsArrayBuffer(myFile);
reader.onabort = () =>{console.log("file reader aborted")}
reader.onerror = () =>{console.log('error with file reader')}
//Call axios within the onload event
reader.onload = () =>{
const arrayBuffer = reader.result; //our array buffer
axios.post(uri, arrayBuffer, {headers})
.then(result=>{
// Do as you please with the result {uuid} of image
console.log(result)})
.catch(error=>{console.log(error)})
}