2015-10-22 63 views
9

我正在使用Resumable.js和Node.js-express应用程序。我能到那里,我认识了文件传输是成功的,因为我可以CONSOLE.LOG(req.body)点,看看下面这...Resumable.js无法将文件保存到服务器端目录

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

的文档是相当含糊后这个做什么点。我需要将这个文件保存到我的服务器上的一个目录。任何有关如何完成此任务的帮助将不胜感激。谢谢!

+0

任何Github的例子将是惊人的.. – Nodedeveloper101

+0

好的,该文件本身位于'req.files.file'...所以我有一个JavaScript变量这是一个文件.. var file = req.files.file; ....我如何将这个变量作为一个文件保存到我的服务器上的特定目录? – Nodedeveloper101

+0

我想出了如何使用'fs'将.txt文件写入服务器。所以我可以写文件到服务器。我可以console.log()有关文件的数据。但无法弄清楚什么对象我必须传递到fs.writeFile(文件名,文件,回调)这实际上是文件...现在它只是保存[对象对象] – Nodedeveloper101

回答

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); 

     } 
    }; 
4

不是专家,但是,我确实试了一下,得到了一个适合你的例子。您的问题中没有提供足够的信息,因此我将从头开始。

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

我基本上需要改变其中一个样品的app.js为了能够上传从浏览器的文件,然后保存在本地。

  1. 通行证目录位置可恢复该文件块应该被添加
  2. 提供必要的条件和命令,这样,当块被上传,创建写入流,然后使用可恢复到块缝合在一起,并将它们保存作为原始文件。
  3. 最后删除所有块

说明使用gist得到明确,应用程序的工作(你可以检出官方GitHub的样品并进行修改,或者你可以按照下面的说明):

  1. 创建一个目录
  2. 在目录中创建的要旨app.js,resumable.js和可恢复-的node.js
  3. 复制/粘贴package.json并做了NPM安装以获得该模块(或手动npm install这些expresspathfsconnect-multiparty
  4. 在主应用程序目录下创建uploads目录,并确保它是可写的
  5. 复制/过去从herepublic目录包含icons的形式.png文件和用于上传的style.cssindex.html
  6. 更改索引的行4950中的target值。html在公共目录中,你刚刚复制到你的服务器地址在我的情况下http://localhost:3000/upload
  7. 你可以调整块大小,同时块上传的数量等在index.html但我把他们作为默认(实际上,我将块从3更改为4)
  8. 完成上述所有操作,然后您就可以开始了。
  9. 运行的应用程序,即nodemon app.js或节点app.js
  10. 浏览到您的服务器地址或http://localhost:3000/upload,你会看到呈现的index.html,如下图所示:

enter image description here

下面控制台日志(注意端 - 块移除)

enter image description here

甲nd最后我在上传目录中有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',...)看到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': '*' 
    }); 
    }); 
}); 

最后最后的调整到app.js是在最后的路由处理,以下文件

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

我移动的文件resumable.js到同一目录别人,所以我需要调整它的位置以便createReadStream找到它。

相关问题