Coder Social home page Coder Social logo

file-poster's Introduction

file-poster

Purpose

A simple 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-poster

Or manually:

git clone git://github.com/blparker/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 disc
  • requestOptions - A string or array representing either the URL, or HTTP request options for a request
  • callback - A function that is invoked after completion. The callback function accepts to parameters:
    • err - Represents the error returned (if an error occurred). Otherwise, null
    • response - The HTTP response returned from the request

file-poster's People

Contributors

blparker avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

benjamingarcia

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.