Resumable.js不能将文件保存到服务器端目录

我用我的Node.js-express应用程序使用Resumable.js。 我可以达到的地步,我知道文件传输是成功的,因为我可以console.log(req.body),看到下面的…

{ resumableChunkNumber: '77', resumableChunkSize: '1048576', resumableCurrentChunkSize: '1064195', resumableTotalSize: '80755971', resumableType: '', resumableIdentifier: '80755971-humanfastq', resumableFilename: 'human.fastq', resumableRelativePath: 'human.fastq', resumableTotalChunks: '77' } 

在这之后的文件是相当模糊的。 我需要将这个文件保存到我的服务器上的一个目录。 任何关于如何做到这一点的帮助将不胜感激。 谢谢!

不是专家,但我确实试了一下,为你效劳。 你没有提供足够的信息在你的问题,所以我要从头开始。

我跟着官方github回购和提供的样本,我不得不做一些调整,以获得保存在一个特定的目录中的文件块,然后更多的调整缝合这些块回来,更多的调整,以删除不必要的块后。 显然,所有这些调整都是代码中提供的提示,以及我浏览过的其他地方。 似乎flow.js也使用了类似的机制(不完全确定)。

我基本上需要更改其中一个示例中的app.js ,以便能够从浏览器上载文件,然后将其保存到本地。

  1. 通过目录位置可以恢复文件块应该添加的位置
  2. 提供必要的条件和命令,以便在上传块时创build一个写入stream,然后使用可恢复的块将块拼接在一起并保存为原始文件。
  3. 最后删除所有的块

说明使用的要点,以获取快速应用程序的工作(您可以检出官方github示例,并进行更改,或者您可以按照下面的说明):

  1. 创build一个目录
  2. 从目录中创buildapp.js,resumable.js和resumable-node.js
  3. 复制/粘贴package.json并执行npm install以获取模块(或手动npm install这些expresspathfsconnect-multiparty
  4. 在主应用程序目录中创builduploads目录,并确保它是可写的
  5. 从这里复制/通过public目录,其中包含.png文件和style.cssindex.htmlforms的icons以供上传
  6. 在你刚刚复制到你的服务器地址的公共目录index.html的第4950行中,改变target http://localhost:3000/upload
  7. 你可以在index.html中调整chunk的大小,并发块的上传数量等等,但是我把它们作为默认值(实际上,我把大块从3改为4)
  8. 完成以上所有,那么你很好走。
  9. 运行应用程序即nodemon app.js或节点app.js
  10. 导航到您的服务器地址或http:// localhost:3000 / upload ,您将看到如下所示呈现的index.html:

在这里输入图像描述

下面的控制台日志(注意结束 – 块删除。)

在这里输入图像描述

最后我在上传目录中有image2.jpg

TL; DR

您需要对app.js和index.html文件进行以下调整才能通过Resumable.js上传和保存上传的文件。

index.html已经调整,以改变目标值指向它的服务器的url在我的情况下http:// localhost:3000 / upload

 var r = new Resumable({ target:'http://localhost:3000/upload', chunkSize:1*1024*1024, simultaneousUploads:4, testChunks:false, throttleProgressCallbacks:1 }); 

app.js已经调整为发送resumable文件的目录(最上面)。

 var resumable = require('./resumable-node.js')(__dirname + "/uploads"); 

我也调整了app.js来改变app.post('/uploads',...)

 // Handle uploads through Resumable.js app.post('/upload', function(req, res){ resumable.post(req, function(status, filename, original_filename, identifier){ if (status === 'done') { var stream = fs.createWriteStream('./uploads/' + filename); //stich the chunks resumable.write(identifier, stream); stream.on('data', function(data){}); stream.on('end', function(){}); //delete chunks resumable.clean(identifier); } res.send(status, { // NOTE: Uncomment this funciton to enable cross-domain request. //'Access-Control-Allow-Origin': '*' }); }); }); 

最后最后调整到app.js是在最后一个路由处理器,下面的文件

 s.createReadStream("./resumable.js").pipe(res); 

我将文件resumable.js移动到其他目录,所以我需要调整它的位置,以便createReadStreamfind它。

我没有使用Resumable.js,但类似的东西,这是我如何在Nodejs / express服务器。 你也可以看看更多的服务器代码在这里: https : //github.com/MichaelLeeHobbs/roboMiner/tree/master/server/api/world但是请记住,它不是完整的,是实验性的。 下面的代码只是一个简单的例子。

  import move from '../../libraries/fsMove.js'; export function create(req, res) { var file = req.files.file; console.log(file.name); console.log(file.type); console.log(file.path); console.log(__dirname); move(file.path, __dirname + '/../../uploads/worlds/' + file.originalFilename, function(err){ if (err) { console.log(err); handleError(res)(err); return; } World.createAsync(req.body) .then(responseWithResult(res, 201)) .catch(handleError(res)); }); } // fsMove.js // http://stackoverflow.com/questions/8579055/how-i-move-files-on-node-js/29105404#29105404 var fs = require('fs'); module.exports = function move (oldPath, newPath, callback) { fs.rename(oldPath, newPath, function (err) { if (err) { if (err.code === 'EXDEV') { copy(); } else { callback(err); } return; } callback(); }); function copy () { var readStream = fs.createReadStream(oldPath); var writeStream = fs.createWriteStream(newPath); readStream.on('error', callback); writeStream.on('error', callback); readStream.on('close', function () { fs.unlink(oldPath, callback); }); readStream.pipe(writeStream); } };