home
github
npm
09 Jun 2024

http-file-upload

http-file-upload is a http server whose only function is to upload and download files ( of any size )
the http server has been designed as a single, no dependency, file for ease of use and portability
files can be uploaded and downloaded
  • using the built browser interface
  • by a http request, such as those made through node.js, php, curl, python, c++, c#, java

install

npm global installation

recommended way to install
npm install http-file-upload -g
http-file-upload can then be run from anywhere on the file system by entering on the command line
http-file-upload -version
note : windows powershell users
running powershell scripts are disabled by default
to get the script to run normally in powershell users will have to delete http-file-upload.ps1 file from the default npm script installation directory this is usually
c:/users/<user>/AppData/Roaming/npm/
or launch the program using
http-file-upload.cmd -version

node-x

i also have another utility for running node.js scripts : node-x
npm : node-x
install locally with npm
npm install http-file-upload
if the directory is not already accessible to node-x, add the current directory
node-x -add
then run from anywhere on the command line with
node-x http-file-upload.js -version
see also alternative installation methods below

uninstall

npm global installation

npm uninstall http-file-upload -g

operation

network interface / port

the server listens on all network interfaces
the default port is 3000

user interface

the built in user interface is accessible, on all network interfaces
https://127.0.0.1:3000/ https://127.0.0.1:3000/hello

command line parameters

http-file-upload supports the following command line parameters
the parameters can be specified in any order, with or without the dash or with a double dash
-p | -port <port> sets the port the server listens on
-d | -dir
<dir>
sets a directory to put new uploads or where to find files for download, its relative to the serving directory, if the directory does not exist it is created
-cwd <directory> sets the current working directory for the script
-https | -http set whether the server uses https ( default ) or http
-cert <cert-file> <key-file> specify a public certificate to use, pem format, the current certificate and key will be replaced and written to the file
generate https certificates
-v | -version prints the current version of the program
-h | -help command line quick help

download url

download a file using a http request at url
https://127.0.0.1:3000/download?<filename>

download files list

a list of all files available to download is available from the following url, data in JSON format is returned
https://127.0.0.1:3000/download-list {files:["a.txt","b.txt"]}

upload url

upload a file to this url using a http request, the upload body must be a binary file https://127.0.0.1:3000/upload?<filename>

quit

http-file-upload can be quit from the command line by pressing
escape / q / ctrl-c
or by using the quit icon in the built in user interface

launch

if http-file-upload is accessible globally, and you have some files you want to access over http in a directory /work/tmp/
  1. change to directory /work/tmp/
  2. type http-file-upload press enter

example useage

upload/download files in the current directory

http-file-upload

upload/download files using port 4000

http-file-upload -p 4000

download a txt file in node.js

var url = 'https://localhost:3000/download?myfile.js'; var opts = {rejectUnauthorized:false}; require('https').get(url,opts,async res=>{ // node >v20 var body = ''; for await(data of res)body += data; console.log(body); // not using for await ... var body = ''; res.on('data',data=>body+=data); res.on('end',()=>console.log(body)); });

download a large binary file in node.js

var url = 'https://localhost:3000/download?video.mp4'; var opts = {rejectUnauthorized:false}; require('https').get(url,opts,async res=>{ var fd = require('fs').createWriteStream('/tmp/video.mp4'); res.pipe(fd); });

download all files in a directory in node.js

var url = 'https://localhost:3000/'; var opts = {rejectUnauthorized:false}; var https = require('https'); https.get(`${url}download-list`,opts,async res=>{ var body = ''; res.on('data',data=>body+=data); res.on('end',()=>{ var json = JSON.parse(body); json.files.forEach(file=>{ https.get(`${url}download?${file}`,opts,res=>{ var fd = fs.createWriteStream(file); res.pipe(fd); }); }); }); });

upload a file in node.js

var url = 'https://localhost:3000/upload?my-file.txt'; var body = require('fs').readFileSync('my-file.txt'); var req = require('https').request(url,{method:'post',rejectUnauthorized:false},rec); req.write(body); req.end(); req.on('error', function(err){ console.log(err); }); async function rec(res){ var body = ''; res.on('data',data=>body+=data); res.on('end',()=>console.log(body)); }//rec

upload a large binary file in node.js

var fd = require('fs').createReadStream('video.mp4'); var url = 'https://localhost:3000/upload?video.mp4'; var opts = {method:'post',rejectUnauthorized:false}; var req = require('https').request(url,opts,rec); req.on('error',err=>console.log(err)); fd.pipe(req); async function rec(res){ var body = ''; res.on('data',data=>body+=data); res.on('end',()=>console.log(body)); }//rec

upload a file from the browser

remember to accept the server cert in the browser
var input = document.createElement('input'); input.type = 'file'; input.onchange = onchange; input.click(); async function onchange(e){ var file = input.files[0]; var url = `https://localhost:3000/upload?${file.name}`; var res = await fetch(url,{method:'post',body:file}); var txt = await res.text(); console.log(txt); }//onchange

download a file with curl

curl --insecure https://localhost:3000/download?my-file.txt

serving files from an alternate directory

if you installed http-file-upload locally with npm at /work/http-file-upload/ and wish to access files at /work/tmp/
npx http-file-upload -cwd /work/tmp/

single file http-file-upload.js

/work/http-file-upload/ and wish to access files at /work/tmp/
node http-file-upload -cwd /work/tmp/

update https certificate

you can download certificates from generate https certificates
the files can be given in either order and the key and cert must be pem encoded
http-file-upload -cert server-cert.pem private-key.pem

using http

http-file-upload -http

alternate installation methods

npm local install

npm install http-file-upload
this will download http-file-upload to ./node_modules/http-file-upload
http-file-upload can then be run using the command, from the directory which you issued the npm install command
npx http-file-upload -version

install from github

download the project files as a zip file from github
http-file-upload.zip
unzip the file, then either run locally or add the launch script to the system path
download the single file
http-file-upload.js
http-file-upload can then be run by
node http-file-upload -version

system path environment variable

if you would like http-file-upload to be accessible from anywhere on the file system, the directory http-file-upload/ext/ should be added to the system path, http-file-upload comes with the following shell scripts to launch the process :
windows ............ http-file-upload/ext/http-file-upload.bat mac ................ http-file-upload/ext/http-file-upload.sh linux .............. http-file-upload/ext/http-file-upload.sh

feedback

stackoverflow chat

im usually available in the stackoverflow javascript chat room user matt, come and say hi, id love to hear from you
page title designed with
textstudio.com