Resumable.js无法将文件保存到服务器端目录

Nod*_*101 11 javascript file save transfer node.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' }
Run Code Online (Sandbox Code Playgroud)

关于在此之后要做什么,文档相当模糊.我需要将此文件保存到我服务器上的目录中.任何有关如何实现这一目标的帮助将不胜感激.谢谢!

Raf*_*Raf 6

不是专家,但我确实尝试过,并为您提供了一个示例。您在问题中没有提供足够的信息,所以我将从头开始。

我遵循了官方github 存储库和提供的示例,我必须进行一些调整才能将文件块保存在特定目录中,然后进行更多调整以将这些块重新拼接在一起,并进行更多调整以在之后删除不必要的块。显然,所有这些调整都是在代码和我浏览的其他地方提供的提示。显然flow.js也在使用类似的机制(不完全确定)。

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

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

使用gist使 express-app 工作的说明(您可以查看官方 github 示例并进行更改,或者您可以按照以下说明进行操作):

  1. 创建目录
  2. 从目录中的 gist 创建 app.js、resumable.js 和 resumable-node.js
  3. 复制/粘贴package.json并执行 npm install 以获取模块(或手动npm install这些express, path, fs, connect-multiparty
  4. uploads在主应用程序目录中创建目录并确保它是可写的
  5. public此处复制/过去包含icons.png 文件形式style.cssindex.html用于上传的目录
  6. 在我的情况下,更改您刚刚复制到您的服务器地址的公共目录中 index.html或index.htmltarget行中的值4950http://localhost:3000/upload
  7. 您可以在 index.html 中调整块大小、同时上传块的数量等,但是,我将它们保留为默认值(实际上,我将块从 3 更改为 4)
  8. 完成以上所有操作,然后您就可以开始了。
  9. 运行应用程序,即 nodemon app.js 或 node app.js
  10. 导航到您的服务器地址或http://localhost:3000/upload,您将看到 index.html 呈现如下所示:

在此处输入图片说明

下面的控制台日志(注意结尾 - 删除了块。)

在此处输入图片说明

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

TL; 博士

您需要对 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
});
Run Code Online (Sandbox Code Playgroud)

app.js已被调整以发送resumable保存文件的目录(最顶部)。

var resumable = require('./resumable-node.js')(__dirname + "/uploads");
Run Code Online (Sandbox Code Playgroud)

我还调整了app.js以更改app.post('/uploads',...)see gist的内容。

// 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': '*'
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

最后对app.js 的最后调整是在最后一个路由处理程序,以下文件

s.createReadStream("./resumable.js").pipe(res);
Run Code Online (Sandbox Code Playgroud)

我将文件 resumable.js 移动到与其他文件相同的目录,所以我需要调整更改它的位置,以便 createReadStream 找到它。


Mic*_*bbs 1

我没有使用 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);

      }
    }; 
Run Code Online (Sandbox Code Playgroud)