2

出于某种原因,当我尝试在本地主机(Windows 7)上写入文件时,writestream 不会打开。在 linux 机器上,它工作正常。我需要在 Windows 中添加某种类型的权限吗?

我已经以管理员身份运行。

这是当前的方法。

// Mainfunction to recieve and process the file upload data asynchronously
var uploadFile = function(req, targetdir,callback) {
  var  total_uploaded = 0
      ,total_file;
    // Moves the uploaded file from temp directory to it's destination
    // and calls the callback with the JSON-data that could be returned.
    var moveToDestination = function(sourcefile, targetfile) {
        moveFile(sourcefile, targetfile, function(err) {
            if(!err)
                callback({success: true});
            else
                callback({success: false, error: err});
        });
    };

    // Direct async xhr stream data upload, yeah baby.
    if(req.xhr) {
        var fname = req.header('x-file-name');
        // Be sure you can write to '/tmp/'
        var tmpfile = '/tmp/'+uuid.v1();
        total_file = req.header('content-length');
        // Open a temporary writestream
        var ws = fs.createWriteStream(tmpfile);
        ws.on('error', function(err) {
            console.log("uploadFile() - req.xhr - could not open writestream.");
            callback({success: false, error: "Sorry, could not open writestream."});
        });
        ws.on('close', function(err) {
            moveToDestination(tmpfile, targetdir+fname);
        });


        // Writing filedata into writestream
        req.on('data', function(data,t,s) {
          ws.write(data,'binary',function(r,e){
            total_uploaded = total_uploaded+e;
            var feed = {user:'hitesh',file:fname,progress:(total_uploaded/total_file)*100};
            require('./../../redis').broadCast(JSON.stringify(feed))
          });
        });

        req.on('end', function() {
            ws.end();
        });
    }

    // Old form-based upload
    else {

        moveToDestination(req.files.qqfile.path, targetdir+req.files.qqfile.name);
    }
};
4

2 回答 2

2

由于您的代码在 Linux 上运行良好,因此它必须是特定于 Windows 的。

var tmpfile = '/tmp/'+uuid.v1();

可能是你的问题。Windows 上的文件夹/路径结构不同。尝试使用该path模块并将您的代码更改为

var path = require('path');

var tmpfile = path.join('tmp', uuid.v1());

您的参数可能也是如此targetdir

看到这个相关的问题。

于 2012-11-19T10:09:11.993 回答
1

问题出在目录上。除非您有 C:\tmp 目录(假设您从 C 驱动器运行节点),否则它没有任何地方可以写入 tmp 文件。

您可以创建一个 C:\tmp 目录或修改该行

var tmpfile = '/tmp/'+uuid.v1();

类似于

var tmpfile = __dirname + '/tmp/'+ uuid.v1();

注意:需要一个类似C:\mynodeproject\tmp的目录

于 2012-12-11T22:14:17.103 回答