1.0.1 • Published 4 years ago

upload-build v1.0.1

Weekly downloads
1
License
ISC
Repository
-
Last release
4 years ago

upload-build

node file upload

Installation

$ npm i upload-build

Usage

    // node
    var http=require("http");
    var uploadBuild=require("./upload-build");
    var upload=uploadBuild(options);

    http.createServer(function(request,response){
        var result=await upload(request);
        console.log(result);
    });
    // koa
    var uploadBuild=require("./upload-build");
    var upload=uploadBuild(options);

    router.post("/upload",async ctx=>{
        let result=await upload(ctx.req);

        console.log(result);
    });

OPTIONS

interface IUploadConfig {
    savePath: string,             
    urlPath:string,                     
    singleFileMaxSize: number,          // 单个文件最大尺寸, default -1
    maxUploadNum:number,                // 最大上传文件数量,default -1
    mineType: Array<string>|null
}
interface IUploadResult {
    fields:{
        [key:string]:any
    },
    uploadCount:number,
    files:IUploadFile[]|null
}
interface IUploadFile {
    name: string,          
    fileName: string,      
    mime: string,
    size: number,
    saveName: string,
    savePath:string,          // config.savePath + saveName
    url:string,               // config.urlPath + savePath
    error: number
}
// IUploadFile.error
enum status {
    SUCCESS = 0,        // 0:成功
    FILE_SIZE_MAX = 1,  // 1:文件超大
    TYPE_MISMATCH = 2,  // 2:非指定类型文件
    NONE = 9            // 9:未选择上传文件
}

uploadBuild.status=status;

example

   var upload=uploadBuild({
        savePath: "./upload",
        urlPath: "http://www.wuweierwei.com/xxx/upload",
        singleFileMaxSize: 1024 * 500,
        maxUploadNum: 10,
        mineType: ["iamge/jpg", "image/jpeg", "image/png"]
    });