upload.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. var fs = require("fs");
  2. var http = require("http");
  3. var path = require("path");
  4. function getReq(options) {
  5. var options = options || {
  6. host: "localhost",
  7. port: "5000",
  8. method: "POST",
  9. path: "/upload"
  10. };
  11. var req = http.request(options, function(res) {
  12. console.log("RES:" + res);
  13. console.log("STATUS: " + res.statusCode);
  14. console.log("HEADERS: " + JSON.stringify(res.headers));
  15. res.on("data", function(chunk) {
  16. console.log("BODY:" + chunk);
  17. });
  18. });
  19. req.on("error", function(e) {
  20. console.log("problem with request:" + e.message);
  21. console.log(e);
  22. });
  23. return req;
  24. }
  25. function readFileList (_path, filesList) {
  26. var stat = fs.statSync(_path);
  27. if(stat.isDirectory()) {
  28. var files = fs.readdirSync(_path);
  29. files.forEach(function (itm, index) {
  30. var stat = fs.statSync(_path + "/" + itm);
  31. if (stat.isDirectory()) {
  32. readFileList(_path + "/" + itm, filesList) //递归读取文件
  33. }
  34. else {
  35. var obj = {};
  36. obj.urlKey = itm
  37. obj.urlValue = _path + "/" + itm; //全路径
  38. filesList.push(obj);
  39. }
  40. })
  41. }
  42. else {
  43. var pathObj = path.parse(_path);
  44. var obj = {};
  45. obj.urlKey = pathObj.base;
  46. obj.urlValue = _path;
  47. filesList.push(obj);
  48. }
  49. }
  50. /**
  51. * 获取文件列表
  52. */
  53. var listFiles = (_path) => {
  54. var filesList = [];
  55. readFileList(_path, filesList);
  56. return filesList;
  57. }
  58. /**
  59. * 上传文件
  60. */
  61. var sendFiles = (fileKeyValue, options) => {
  62. var req = getReq(options);
  63. var boundaryKey = Math.random().toString(16);
  64. var enddata = "\r\n----" + boundaryKey + "--";
  65. var files = new Array();
  66. for (var i = 0; i < fileKeyValue.length; i++) {
  67. var content =
  68. "\r\n----" +
  69. boundaryKey +
  70. "\r\n" +
  71. "Content-Type: application/octet-stream\r\n" +
  72. 'Content-Disposition: form-data; name="' +
  73. fileKeyValue[i].urlKey +
  74. '"; filename="' +
  75. path.basename(fileKeyValue[i].urlValue) +
  76. '"\r\n' +
  77. "Content-Transfer-Encoding: binary\r\n\r\n";
  78. //当编码为ascii时,中文会乱码。
  79. var contentBinary = new Buffer(content, "utf-8");
  80. files.push({
  81. contentBinary: contentBinary,
  82. filePath: fileKeyValue[i].urlValue
  83. });
  84. }
  85. var contentLength = 0;
  86. for (var i = 0; i < files.length; i++) {
  87. var stat = fs.statSync(files[i].filePath);
  88. contentLength += files[i].contentBinary.length;
  89. contentLength += stat.size;
  90. }
  91. req.setHeader(
  92. "Content-Type",
  93. "multipart/form-data; boundary=--" + boundaryKey
  94. );
  95. req.setHeader("Content-Length", contentLength + Buffer.byteLength(enddata));
  96. //将参数发出
  97. var fileindex = 0;
  98. var doOneFile = function() {
  99. req.write(files[fileindex].contentBinary);
  100. var fileStream = fs.createReadStream(files[fileindex].filePath, {
  101. bufferSize: 4 * 1024
  102. });
  103. fileStream.pipe(req, { end: false });
  104. fileStream.on("end", function() {
  105. fileindex++;
  106. if (fileindex == files.length) {
  107. req.end(enddata);
  108. } else {
  109. doOneFile();
  110. }
  111. });
  112. };
  113. if (fileindex == files.length) {
  114. req.end(enddata);
  115. }
  116. else {
  117. doOneFile();
  118. }
  119. }
  120. module.exports = {
  121. 'listFiles' : listFiles,
  122. 'sendFiles' : sendFiles
  123. }
  124. /**
  125. //读取文件列表
  126. let files = [];
  127. readFileList("/Users/siena/Desktop/image", files);
  128. //上传文件
  129. postFile(files, getReq());
  130. */