Merge pull request #3316 from kenfdev/master

added multiparty definition file
This commit is contained in:
Masahiro Wakame
2014-12-18 12:47:01 +09:00
2 changed files with 183 additions and 0 deletions
+72
View File
@@ -0,0 +1,72 @@
/// <reference path='multiparty.d.ts' />
/// <reference path='../node/node.d.ts' />
import multiparty = require('multiparty');
import http = require('http');
import util = require('util');
http.createServer(function (req: http.ServerRequest, res: http.ServerResponse) {
if (req.url === '/upload' && req.method === 'POST') {
var count = 0;
var form = new multiparty.Form();
// Errors may be emitted
// Note that if you are listening to 'part' events, the same error may be
// emitted from the `form` and the `part`.
form.on('error', function (err: Error) {
console.log('Error parsing form: ' + err);
});
// Parts are emitted when parsing the form
form.on('part', function (part: multiparty.Part) {
// You *must* act on the part by reading it
// NOTE: if you want to ignore it, just call "part.resume()"
if (!!part.filename) {
// filename is exists when this is a file
count++;
console.log('got field named ' + part.name + ' and got file named ' + part.filename);
// ignore file's content here
part.resume();
} else {
// filename doesn't exist when this is a field and not a file
console.log('got field named ' + part.name);
// ignore field's content
part.resume();
}
part.on('error', function (err: Error) {
// decide what to do
console.log('Error on part event: ' + err);
});
});
form.on('progress', function (bytesReceived: number, bytesExpected: number) {
// decide what to do
console.log('BytesReceived: ' + bytesReceived, 'BytesExpected: ', bytesExpected);
});
form.on('field', function (name: string, value: string) {
// decide what to do
console.log('Field Name: ' + name + ', Field Value: ' + value);
});
// Close emitted after form parsed
form.on('close', function () {
console.log('Upload completed!');
res.end('Received ' + count + ' files');
});
// Parse req
form.parse(req);
}
// show a file upload form
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">' +
'<input type="text" name="title"><br>' +
'<input type="file" name="upload" multiple="multiple"><br>' +
'<input type="submit" value="Upload">' +
'</form>'
);
}).listen(8080);
+111
View File
@@ -0,0 +1,111 @@
// Type definitions for node-multiparty
// Project: https://github.com/andrewrk/node-multiparty
// Definitions by: Ken Fukuyama <https://github.com/kenfdev>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path='../node/node.d.ts' />
declare module "multiparty" {
import http = require('http');
import events = require('events');
import stream = require('stream');
export class Form extends events.EventEmitter {
constructor(options?: FormOptions);
/**
* Parses an incoming node.js request containing form data.
* This will cause form to emit events based off the incoming request
* @param request
* @param callback
*/
parse(request: http.ServerRequest, callback?: (error: Error, fields: any, files: any) => any): void;
}
export interface File {
/**
* same as name - the field name for this file
*/
fieldName: string;
/**
* the filename that the user reports for the file
*/
originalFileName: string;
/**
* the absolute path of the uploaded file on disk
*/
path: string;
/**
* the HTTP headers that were sent along with this file
*/
headers: any;
/**
* size of the file in bytes
*/
size: number;
}
interface Part extends stream.Readable {
/**
* the headers for this part. For example, you may be interested in content-type
*/
headers: any;
/**
* the field name for this part
*/
name: string;
/**
* only if the part is an incoming file
*/
filename: string;
/**
* the byte offset of this part in the request body
*/
byteOffset: number;
/**
* assuming that this is the last part in the request, this is the size of this part in bytes.
* You could use this, for example, to set the Content-Length header if uploading to S3.
* If the part had a Content-Length header then that value is used here instead.
*/
byteCount: number;
}
export interface FormOptions {
/**
* sets encoding for the incoming form fields. Defaults to utf8.
*/
encoding?:string;
/**
* Limits the amount of memory all fields (not files) can allocate in bytes.
* If this value is exceeded, an error event is emitted. The default size is 2MB.
*/
maxFieldsSize?:number;
/**
* Limits the number of fields that will be parsed before emitting an error event.
* A file counts as a field in this case. Defaults to 1000.
*/
maxFields?:number;
/**
* Only relevant when autoFiles is true.
* Limits the total bytes accepted for all files combined.
* If this value is exceeded, an error event is emitted.
* The default is Infinity.
*/
maxFilesSize?:number;
/**
* Enables field events and disables part events for fields.
* This is automatically set to true if you add a field listener.
*/
autoFields?:boolean;
/**
* Enables file events and disables part events for files.
* This is automatically set to true if you add a file listener.
*/
autoFiles?:boolean;
/**
* Only relevant when autoFiles is true.
* The directory for placing file uploads in.
* You can move them later using fs.rename(). Defaults to os.tmpDir().
*/
uploadDir?:string;
}
}