Git Product home page Git Product logo

node-upload-progress's Introduction

It's a Node.js module to handle upload and upload-progress.

Build Status

##Instal

$ npm install node-upload-progress

or

# package.json
# ...
"dependencies": {
  "node-upload-progress": "latest"
}
# ...

$ npm install

##Usage

###Simple

var app = require('http');
var uploadProgress = require('node-upload-progress');

uploadHandler = new uploadProgress.UploadHandler;

The uploadHandler.uploadDir is the path where the files will be saved. Without this configuration the files will be saved in a path based on process.env.TMP.

uploadHandler.configure(function() {
  this.uploadDir = __dirname + '/uploads';
});

app.createServer(function(req, res) {
  if (req.url === '/upload') {
    uploadHandler.upload(req, res);
    return;
  }
  # ...
}

####Custom response body

Configuring the uploadHandler.onEnd = customOnEndHandler, you can write your own response body.

# ...
uploadHandler.configure(function() {
  this.uploadDir = "" + __dirname + "/uploads";
  this.onEnd = customOnEndHandler;
});

function customOnEndHandler(req, res){
  res.writeHead 200, {'Content-Type': 'text/plain'}
  res.end('Upload received');
}

app.createServer(function(req, res) {
  if (req.url === '/upload') {
    uploadHandler.upload(req, res);
    return;
  }
  # ...
}

See a full example at examples/simple.

##Progress

If you want to use an upload progress similar to Nginx Upload Progress Module, you can easily do it using the progress handler.

# ...
app.createServer(function(req, res) {
  if (req.url.match(/\/upload\?X\-Progress\-ID=.+/)) {
    uploadHandler.upload(req, res);
    return;
  } else if (req.url.match(/\/progress\?X\-Progress\-ID=.+/)) {
    uploadHandler.progress(req, res);
    return;
  }
  # ...
}

###The view

The Javascript

<script>
	$(function(){
		$('#form_upload').submit(function(){
			var xProgressID = guidGenerator();
			$(this).attr('action', '/upload?X-Progress-ID=' + xProgressID);
			var uploadIntervalID = setInterval(function(){
				$.get('/progress?X-Progress-ID=' + xProgressID, function(data){
					if(data.status === 'done'){
						clearInterval(uploadIntervalID);
					}
					updateViewUploadStatus(data);
				}).error(function(){clearInterval(uploadIntervalID)});
			}, 250);
			return true;
		});
		function updateViewUploadStatus(data){
			# ...
		}
		// http://stackoverflow.com/a/105074/464685
		function guidGenerator() {
			# ...
		}
	});
</script>

####The HTML

# ...
<form action="/upload?X-Progress-ID=1" enctype="multipart/form-data" method="post" id="form_upload" target="iframe_upload">
	<p>
		<label>File</label><br/>
		<input type="file" name="upload" id="upload"><br>
	</p>
	<p>
		<input type="submit" value="Upload">
	</p>
</form>
<iframe id="iframe_upload" name="iframe_upload"></iframe>
# ...

See a full example at examples/progress.

###The possible status

The upload request hasn't been registered yet or is unknown:

HTTP 404 Not Found

The upload request has ended:

{"bytesReceived":N,
 "bytesExpected":N,
 "percent":100,
 "status":"done",
 "fileName":"filename.txt",
 "filePath":"uploadDir/filename.txt"}

The upload request is in progress:

{"bytesReceived":N,"bytesExpected":N,"percent":N,"status":"uploading"}

##Running it

###Simple example

$ make simple

Then

open http://localhost:8080

###Progress example

$ make progress

Then

open http://localhost:8080

###Test suite

$ make test

node-upload-progress's People

Watchers

M N Islam Shihan avatar  avatar

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.