You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A simple form factory for both node and the browser
Describe forms using json objects
Multipart/formdata
UTF-8
File upload
Streamed in the node version
How to use it ?
With node
npm install formFactory
//Amazon S3 POST examplevarhttps=require('https');varurl=require('url');varfs=require('fs');vars3Endpoint='https://s3-us-west-2.amazonaws.com/42';varpath='./images/john.png'varformFactory=require('formFactory');varform=formFactory({key: 'images/john.png',AWSAccessKeyId: 'AKIAIAVVU5KANH5LYROZ','Content-Type': 'image/png',success_action_status: 201,acl: 'private','x-amz-meta-filename': 'john.png',policy: 'eyJleHBpcmF0aW9uIjoiMjAxNS0wN...',signature: 'rdvA5qlKbSry/W1JuwKoZHvulhc='},{//Please note that file key could be any key//and will be used as field name for the filefile: {stream: fs.createReadStream(path),name: path.split('/').pop(),type: 'image/png',size: fs.statSync(path).size}});varheaders=form.headers;varbody=form.body;//Stream//Send the requestvaroptions=url.parse(s3Endpoint);options.headers=headers;options.method='POST';varrequest=https.request(options,function(res){if(res.statusCode===201){console.log('file saved to S3 !');}});body.pipe(request);
In the browser
bower install formFactory
<!DOCTYPE html><html><head><!-- Amazon S3 POST example --><scriptsrc="bower_components/formFactory/browserVersion.js"></script><script>vars3Endpoint='https://s3-us-west-2.amazonaws.com/42';varinput=document.getElementById('file');//Wait for user selectioninput.addEventListener('change',function(evt){varfile=evt.target.files[0];//Create formvarform=formFactory({key: 'images/john.png',AWSAccessKeyId: 'AKIAIAVVU5KANH5LYROZ','Content-Type': 'image/png',success_action_status: 201,acl: 'private','x-amz-meta-filename': 'john.png',policy: 'eyJleHBpcmF0aW9uIjoiMjAxNS0wN...',signature: 'rdvA5qlKbSry/W1JuwKoZHvulhc='},{file: file});//Send the requestvarxhr2=newXMLHttpRequest();xhr2.addEventListener('load',function(){console.log('file saved to S3 !');});xhr2.open('POST',s3Endpoint);xhr2.send(form);});</script></head><body><inputtype="file" id="file"></body></html>