git clone https://github.com/NY0510/cli-file-upload.git
cd cli-file-upload
npm install
Create .env
file in the root of the project.
UPLOAD_FOLDER='./file/' # Folder to store uploaded files
MAX_UPLOAD_SIZE=10 # Max file size in MB
TOKEN='supersupersecretny64token' # Token to authenticate requests
BASE_URL='http://localhost' # Base URL of the server
PORT=3000 # Port to run the server on
And run the server with the command npm start
curl /X POST URL_OF_SERVER /F 'file=@FILE_NAME'
curl -X POST URL_OF_SERVER -F 'file=@FILE_NAME'
If you want to upload multiple files, you can use -F file=@FILE_NAME'
or /F file=@FILE_NAME'
several times.
If you upload a secret token with it included in the header, you can save it while preserving the original file name, not the random file name.
-H 'Token: SUPER_SECRET_TOKEN'
, /H 'Token: SUPER_SECRET_TOKEN'
Add this after the command.
Example:
curl -X POST URL_OF_SERVER -F 'file=@FILE_NAME' -H 'Token: SUPER_SECRET_TOKEN'
[
{
"status": "OK",
"originalName": "testfile1.gif",
"fileURL": "http://localhost:3000/files/87e092cbc8f57ca2501578a201e338e567e8.gif",
"fileSize": 965311,
"mimetype": "image/gif"
},
{
"status": "OK",
"originalName": "testfile2.ico",
"fileURL": "http://localhost:3000/files/076df2b8ed466d4c179aedb7724e19721280.ico",
"fileSize": 370070,
"mimetype": "application/octet-stream"
}
]
[
{
"status": "OK",
"originalName": "testfile1.gif",
"fileURL": "http://localhost:3000/files/1643769689678_testfile1.gif",
"fileSize": 965311,
"mimetype": "image/gif"
},
{
"status": "OK",
"originalName": "testfile2.ico",
"fileURL": "http://localhost:3000/files/1643769689689_testfile2.ico",
"fileSize": 370070,
"mimetype": "application/octet-stream"
}
]
{
"status": "ERROR",
"message": "File is too big! Upload size limit is 1MB"
}