gpt4 book ai didi

node.js - Node : Writestream on windows machine

转载 作者:搜寻专家 更新时间:2023-11-01 00:46:21 27 4
gpt4 key购买 nike

出于某种原因,当我尝试在本地主机 (Windows 7) 上写入文件时,写入流无法打开。在 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);
}
};

最佳答案

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

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

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

var path = require('path');

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

您的参数 targetdir 可能也是如此。

参见 this相关问题。

关于node.js - Node : Writestream on windows machine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13447669/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com