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;

Node bak的更多相关文章

  1. Luogu 3369 / BZOJ 3224 - 普通平衡树 - [替罪羊树]

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...

  2. OCP升级(3.6->3.7)

    有个好文档还是靠普很多,感谢同事的文档.升级步骤记录如下 1.检查现有环境 [root@master ~]# etcd --version etcd Version: Git SHA: 1674e68 ...

  3. babeljs源码

    babel.min.js!function(e,t){"object"==typeof exports&&"object"==typeof mo ...

  4. Ubuntu 16.04 64位 搭建 node.js NodeJS 环境

    我的系统环境: Ubuntu 16.04 64位 本文内容亲测可用, 请放心食用 使用淘宝镜像 淘宝镜像官网是https://npm.taobao.org/ 使用淘宝镜像前请自行安装好 npm 和 n ...

  5. svu update 遇到 Node remains in conflict

    http://stackoverflow.com/questions/11774868/svn-checkout-without-restoring up vote4down votefavorite ...

  6. Ubuntu安装node

    #!/bin/bash echo "添加环境变量需要root权限,如无root权限,则不添加环境变量" echo "输入Node下载地址(目前仅支持Node官方网站上Li ...

  7. aix 上搭建node.js 环境

    下载nodejs:ibm-4.4.3.0-node-v4.4.3-aix-ppc64.bin IBM已经适配最新版本的node.js  :https://developer.ibm.com/node/ ...

  8. 优化linux,安装node环境

    就是这样我的心爱的云主机就被攻击了,反正我是很久没宠幸过她,肯定不是我去攻击人家,但是吧昨天就突然来了封邮件,小白一个查不出什么,用了 netsat -na显示所有连接到服务器的活跃的网络连接数,发现 ...

  9. node 文件操作

    对文件的各种操作,使用姿势如下 文件操作单例 @example fu.exist(file); //是否存在 fu.copy(file, 'new-path'); //复制到某个新目录 fu.move ...

随机推荐

  1. GMA Round 1 大吉大利,晚上吃鸡

    传送门 大吉大利,晚上吃鸡 新年走亲访友能干点啥呢,咱开黑吃鸡吧. 这里有32个人,每个人都可能想玩或者不想玩,这样子一共有$2^{32}$种可能.而要开黑当然得4人4人组一队(四人模式),所以说如果 ...

  2. poj3190 Stall Reservations(贪心+STL)

    https://vjudge.net/problem/POJ-3190 cin和scanf差这么多么..tle和300ms 思路:先对结构体x升序y升序,再对优先队列重载<,按y升序. 然后依次 ...

  3. ASP.NET WebAPI数据传输安全HTTPS实战项目演练

    一.课程介绍 HTTPS是互联网 Web 大势所趋,各大网站都已陆续部署了 HTTPS .  全站HTTPS时代,加密用户与网站间的交互访问,在客户端浏览器和Web服务器之间建立安全加密通道,一般情况 ...

  4. 咏南新CS三层开发框架

    咏南新CS三层开发框架 咏南WEB桌面框架演示:47.106.93.126:9999 咏南WEB手机框架本地:47.106.93.126:8077 咏南CS框架下载:https://pan.baidu ...

  5. 如何确定一台linux主机是Linux (i386/i686)还是Linux (x86_64)

    在下软件包的时候,往往会遇到一个选择: 假设自己的主机是Linux,那么Linux (i386/i686)和Linux (x86_64)究竟应该选哪一个呢? 针对当今的硬件而言,如果你主机的CPU是6 ...

  6. PL/SQL学习笔记程序单元

    一:程序单元组成 一个PL/SQL程序单元主要包括三部分: 声明与定义部分:声明变量.常量.类型等:定义过程.函数等: 执行部分:执行PL/SQL语句:调用过程.参数:处理游标等: 异常处理部分:处理 ...

  7. 词向量可视化--[tensorflow , python]

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ ---------------------------------- ...

  8. Linux 搭建git 自己拉取本地 git pull,其他地方的git仓库拉取代码

    Linux 下建立 Git 与 GitHub 的连接 Git 是一款开源的分布式版本控制系统,而 GitHub 是依托 Git 的代码托管平台. GitHub 利用 Git 极其强大的克隆和分支功能, ...

  9. Typora的使用

    Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式,其目标是实现易读易写.我刚刚接触一款简单高效的Markdown编辑器–Typora, ...

  10. Vue Resource root options not used?

    I specify a root options in my Vue-Resource in my main.js file, but when I do the request, it does n ...