Next.js Discord

Discord Forum

Uploading files

Unanswered
Pacific herring posted this in #help-forum
Open in Discord
Pacific herringOP
Hello, I want my users upload files, though I have no idea how to do it. I know that there is <input type="file" id="myFile" name="filename"> but I don't know how to use it, I have no idea how to move myself when the user clicks "submit", what happens etc. Can someone show me please? I'm not asking you to make my code, but too show me a lil snippet.

4 Replies

@Pacific herring Hello, I want my users upload files, though I have no idea how to do it. I know that there is `<input type="file" id="myFile" name="filename">` but I don't know how to use it, I have no idea how to move myself when the user clicks "submit", what happens etc. Can someone show me please? I'm not asking you to make my code, but too show me a lil snippet.
you can archive this by using for example a form: create a form, add the onsubmit function, get the event variable and then get the file (const file = e.target.files[0];). After you got it, you can put it in FormDate (const data = new FormData();), take a look at the docs to know how to use it. After you set it in formdata you can send the formdata to your server (by the way you can also do all the stuff by using server actions) and on serverside you can call the api for uploading stuff. The code depends on your storage provider. If you are using S3 from AWS you can upload it by using the PutObjectCommand with your uploadParams