文件上傳下載

shp文件上傳,formidable

安裝formidable:


npm i -S formidable

安裝unzip模塊

npm i -S unzip

上傳一個(gè)shp壓縮文件至服務(wù)器,在服務(wù)器端解壓后入庫(kù)。
需要注意1、form.uploadDir='path',這個(gè)path是已經(jīng)存在的目錄

 var ls = childProcess.exec('shp2pgsql -c -g geom -D -s 3857 -S -i -I  '+ com_path + '| psql -p 5433 -U postgres -d test');
var fs=require('fs');
var util=require('util');
var path=require('path');
var unzip = require("unzip");
var childProcess=require('child_process');
const formidable = require('formidable');
exports.upload=function (req,res) {
    const form = new formidable.IncomingForm();
    form.encoding = 'utf-8';
    form.uploadDir = "test";
    form.keepExtensions = true;
    fields=[];
    files=[];
    form
        .on('field', function (field, value) {
            //console.log(field, value);
            fields.push([field, value]);
        })
        .on('file', function (field, file) {
           // console.log(field, file);
            files.push([field, file]);
        })
        .on('end', function () {
            console.log('-> upload done');
            res.writeHead(200, {'content-type': 'text/plain'});
            res.write('received fields:\n\n ' + util.inspect(fields));
            res.write('\n\n');
            res.end('received files:\n\n ' + util.inspect(files));
            var ppath=files[0][1].path;
            var tmp_path=ppath.substring(0,ppath.length-4);
            console.log(tmp_path);
            var filename=files[0][1].name.replace('.zip','.shp');
            fs.mkdir(tmp_path, 0777, function(err){
                if(err){
                    console.log(err);
                }else{
                    console.log("creat done!");
                }
            });
            var rootdir=path.dirname(__dirname);
            var com_path=path.join(rootdir,tmp_path,filename);
            var file2=fs.createReadStream(files[0][1].path).pipe(unzip.Extract({ path:tmp_path}));
            console.log('shp2pgsql -c -g geom -D -s 3857 -S -i -I  '+com_path + '| psql -p 5433 -U postgres -d test');
            var ls = childProcess.exec('shp2pgsql -c -g geom -D -s 3857 -S -i -I  '+ com_path + '| psql -p 5433 -U postgres -d test');
        });
    form.parse(req);
}

等價(jià)于:

var fs=require('fs');
var util=require('util');
var path=require('path');
var unzip = require("unzip");
var childProcess=require('child_process');
const formidable = require('formidable');
exports.upload=function (req,res) {
    const form = new formidable.IncomingForm();
    form.encoding = 'utf-8';
    form.uploadDir = "test";
    form.keepExtensions = true;
 form.parse(req, function(err, fields, files) {
        if(err){
            throw err;
        }
        var ppath=files.upload.path;
        console.log(ppath);
        var tmp_path=ppath.substring(0,ppath.length-4);
        var file2=fs.createReadStream(files.upload.path).pipe(unzip.Extract({ path:tmp_path}));
    });
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容