Node bak
process.on('uncaughtException', function (err) { logger.info('Local Server Exception: ') logger.info(err) }); var http = require('http'); var express = require('express'); var bodyParser = require('body-parser') var router = express(); var path = require('path') var uuid = require('node-uuid') var fs = require('fs') // 添加 body-parser 中间件就可以了 /* router.use(express.static(default_pic)) //设置静态文件路径 router.use(express.static(default_temp_pic)); router.use(express.static(default_re_pic)); */ router.use(bodyParser.urlencoded({ extended: false })); router.use(bodyParser.json({limit: '50mb'})); router.post('/test_upload_file_parts', function (req, res) { //data, name , total, index, uuid console.log(req) var data = req.body.buffer; var name = req.body.filename; var total = req.body.total; var index = req.body.index; var uuid = req.body.uuid; console.log(data); console.log(name ,total , index , uuid); if(data && name && total && index && uuid){ try{ var root_dir = "d:\\server_picture"; var temp_dir = path.join(root_dir, uuid); if (!fs.existsSync(temp_dir)) { console.log(`${temp_dir} is no exist, now to create`); fs.mkdirSync(temp_dir); } var temp_name = `${uuid}_${index}`; var temp_file = path.join(temp_dir, temp_name); fs.writeFileSync(temp_file, data); console.log(temp_file) res.send({ 'type': 'AJAX', 'msg': 'ok', body: req.body }); }catch (err){ console.log(err) res.send({ 'type': 'ERROR', 'msg': err.stack }); } } else{ res.send({ 'type': 'ERROR', 'msg': 'data && name && total && index && uuid error' }); } }) router.post('/test_upload_file_merge', function (req, res) { var name = req.body.filename; var total = req.body.total; var uuid = req.body.uuid; console.log(name ,total , uuid); if( name && total && uuid){ try{ var root_dir = "d:\\server_picture"; var temp_dir = path.join(root_dir, uuid); var save_image = path.join(root_dir, name); if(fs.existsSync(save_image)){ fs.unlinkSync(save_image); } var buffer = ''; for(var i=0;i<total;i++){ var index = i + 1; var temp_name = `${uuid}_${index}`; var temp_file = path.join(temp_dir, temp_name); if(fs.existsSync(temp_file)){ console.log(`merge ${i+1}/${total}`) var s_buffer = fs.readFileSync(temp_file); buffer += s_buffer; } } console.log(buffer.length) fs.appendFileSync(save_image, buffer); console.log(save_image) res.send({ 'type': 'AJAX', 'msg': 'ok', body: req.body }); }catch (err){ console.log(err) res.send({ 'type': 'ERROR', 'msg': err.stack }); } } else{ res.send({ 'type': 'ERROR', 'msg': ' name && total && uuid error' }); } }) var server = router.listen(8090, function () { var host = server.address().address var port = server.address().port console.log("the local server http://%s:%s", host, port) })
var path = require('path') var fs = require('fs') var ini = require('ini') //var OSS = require('ali-oss').Wrapper; version 4.5.1 var OSS = require('ali-oss'); //6.0.1 var request = require('request') var request_progress = require('request-progress') var uuid = require('node-uuid') var url = 'http://127.0.0.1:8090/test_upload_file_parts'; var merge_url = 'http://127.0.0.1:8090/test_upload_file_merge'; var filepath = 'D:\\pps.jpg'; var filename = 'pps.jpg'; var props = {}; props._post = function(_url, _params){ return new Promise(function(resolve, reject){ request({ url: _url, method: "POST", json: true, timeout: 20000, headers: { "content-type": "image/jpeg" }, body: _params }, function(error, response, body){ if (!error && response.statusCode == 200) { resolve({'type':'AJAX'}) } else { resolve({'type':'ERROR'}) } }) }) } props.uploadFileParts = async function(_url, _filepath, _filename, _progress_fn){ var buffer = fs.readFileSync(_filepath); var total_size = buffer.length; var shardSize = 120400; // 120kb一个分片 var shardCount = Math.ceil(total_size/shardSize); var _guid = uuid.v1(); _progress_fn(0); for(var i=0;i<shardCount;i++) { var start = i * shardSize; var end = Math.min(start + shardSize, total_size); var params = { buffer: buffer.slice(start, end), filename: _filename, total: shardCount, index: (1 + i), 'uuid': _guid } var hr = await this._post(_url, params); if (hr.type == 'AJAX') { _progress_fn(i / shardCount); } else { return {error: `upload part error`, errorCode: -1}; } } var datas = { filename: _filename, total: shardCount, 'uuid': _guid } //merge var hr = await this._post(merge_url, datas); if(hr.type == 'AJAX'){ return {error: null, errorCode: 200}; _progress_fn(1); }else{ return {error: `upload parts merge error`, errorCode: -2}; } } props.uploadFileParts(url,filepath, filename, function (progress) { console.log(progress); }) /* return new Promise((resolve, reject) => { yq.http.post(url, data, function(res){ resolve(res); }, function (err) { resolve(null); }) }) */ module.exports = props;