- FormData is a built-in object in the browser that allows us to create key-value pairs of data, which can then be sent to the server in a POST request.
- Uses the same format as a form with
enctype="multipart/form-data"
- Continue last exercise. Create a new branch 'upload' with git.
- Add
VITE_UPLOAD_SERVER=https://10.120.32.94/upload/api/v1
to.env.local
file. This is the file server where we will upload the files. - Create
Upload.jsx
toviews
- Add
upload
to routing in App.jsx- Add
Upload
to imports - Add
<Route path="/upload" element={<Upload/>}/>
toRoutes
- Add
<Link to="/upload">Upload</Link>
toNav
inLayout.jsx
- Add
- Add
<form>
to Upload.jsx with file input and submit button:// Upload.jsx const Upload = () => { const [file, setFile] = useState(null); return ( <> <h1>Upload</h1> <form onSubmit={handleSubmit}> <div> <label htmlFor="title">Title</label> <input name="title" type="text" id="title" onChange={handleInputChange} /> </div> <div> <label htmlFor="description">Description</label> <textarea name="description" rows={5} id="description" onChange={handleInputChange} ></textarea> </div> <div> <label htmlFor="file">File</label> <input name="file" type="file" id="file" accept="image/*, video/*" onChange={handleFileChange} /> </div> <img src={ file ? URL.createObjectURL(file) : 'https://via.placeholder.com/200?text=Choose+image' } alt="preview" width="200" /> <button type="submit" disabled={file && inputs.title.length > 3 ? false : true} > Upload </button> </form> </> ); };
- We can use
useForm
hook to handletitle
anddescription
but for file input we need to create a new functionhandleFileChange
:// Upload.jsx const handleFileChange = (evt) => { if (evt.target.files) { console.log(evt.target.files[0]); // TODO: set the file to state } };
- Submitting the form:
// Upload.jsx const doUpload = async () => { try { // TODO: call postFile function (see below) // TODO: call postMedia function (see below) // TODO: redirect to Home } catch (e) { console.log(e.message); } };
- To upload the file to the file server, create a new hook
useFile
toApiHooks.js
. Then add a new functionpostFile
touseFile
hook:
// ApiHooks.js const postFile = async (file, token) => { // TODO: create FormData object // TODO: add file to FormData // TODO: upload the file to file server and get the file data (url = import.meta.env.VITE_UPLOAD_SERVER + '/upload') // TODO: return the file data. };
- Then add new function
postMedia
touseMedia
hook:
// ApiHooks.js const postMedia = async (file, inputs, token) => { // TODO: create a suitable object for Media API: without media_id, user_id, thumbnail and created_at. All those are generated by the API. See the media API documentation. // TODO: post the data to Media API and get the data as MediaResponse // TODO: return the data };
- Finally, add
postFile
andpostMedia
function calls todoUpload
function. - Test the app. Upload a file and check that it is added to the media list.
- To upload the file to the file server, create a new hook
- Run
npm build
ornpm run build
- Move build folder to your public_html
- Test your app:
http://users.metropolia.fi/~username/upload
- Modify README.md. Change the link in
Open [X](X) to view it in the browser.
to point to the above link. - git add, commit & push to remote repository
- Submit the link to correct branch of your repository to Oma