1.0.2 • Published 5 years ago

node-sendfile v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Node-Sendfile

Send a large file to the remote server in synchronous mode and chunked transfer.

Installation

npm install node-sendfile --save

Usage

const Sendfile = require("node-sendfile") 
let sendfile = new Sendfile();
let response = await sendfile.send( "/path/large-file.gz", "https://playground.yaojs.org/yao/upload", { 
        params: { version:1.0 },
        payload:{ name:"hello" },
        progress: ( sent, total ) =>{
            let percent = (sent / total * 100).toFixed(2);
            console.log(`progress ${shouldSent} ${sent}/${total} ${percent}%`);
        }
    });
    console.log( response );

Parameters

NameTypeRequiredDescription
filestringtruethe local file
urlstringtruethe remote server url
optionsobjectfalseSee Options

Options

NameTypeDefaultDescription
namestring"file"The file field name
chunksizeint524288Chunk size, default is 512kb
progressfunction(sent, total)=>{}Callback function for handle progress. Default is (sent, total)=>{}
payloadobject{}The request body Key-Value struct. Default is {}
paramsobject{}The request query string. Key-Value struct. Default is {}
headersobject{}The request header. Key-Value struct. Default is {}

Backend

HTTP Request headers

NameFormat/ValueDescription
Content-Type:multipart/form-data
Content-Name:uM7mW6gByqIhUUN4rO8j9.tarThe unique name of the given file
Content-Range<unit> <range-start>-<range-end>/<size>See Content-Range
Content-Dispositionform-data; name="<field name>"; filename="<file basename>"See Content-Disposition

HTTP Response definition

StatusBodyDescription
200{"path":"/storage/path/large-file.gz", "url":"https://playground.yaojs.org/static-file/path/large-file.gz"}Success. The response body must be JSON format text
400~500{"code":400,"message":"user sex required", "extra":{"fields":"sex", "messages":{"sex":"sex required"}} }User Input error. The response body must be JSON format text
500~600{"code":500,"message":"not enough disk space"} }Backend error. The response body must be JSON format text