0.0.1 • Published 8 years ago
file-post v0.0.1
file-poster
Purpose
A simple fork from blparker/file-poster. It's a node.js module to post a file to remote resource simulating a multipart form.
Installation
You can install file-poster via npm
:
npm install file-post
Or manually:
git clone git://github.com/benjamingarcia/file-poster.git file-poster
Example
Client usage:
var postFile = require('file-poster'),
url = require('url'),
assert = require('assert');
var options = url.parse('http://localhost:8080/upload');
options['method'] = 'POST';
postFile(__dirname + '/kitten.jpg', options, function(err, res) {
assert.equal(res.statusCode, 200);
assert.equal(err, null);
});
Example server (note, this server is using felixge's node-formidable library):
var http = require('http'),
formidable = require('formidable'),
path = require('path');
var server = http.createServer();
server.on('request', function(req, res) {
if(req.method.toLowerCase() === 'post') {
var form = new formidable.IncomingForm(),
fields = [],
files = [];
form
.on('fileBegin', function(name, file) {
file.path = __dirname + '/test' + path.extname(file.name);
})
.on('field', function(field, value) {
fields.push([ field, value ]);
})
.on('file', function(field, file) {
files.push([ field, file ]);
})
.on('end', function() {
res.end();
});
form.parse(req);
}
else {
res.end();
}
});
server.listen(8080);
Usage
postFile(filePath, requestOptions, callback)
filePath
- A string representing the path to a file on discrequestOptions
- A string or array representing either the URL, or HTTP request options for a requestcallback
- A function that is invoked after completion. The callback function accepts to parameters:err
- Represents the error returned (if an error occurred). Otherwise, nullresponse
- The HTTP response returned from the request
0.0.1
8 years ago