使用html表单将文件上传到节点js?

时间:2014-03-22 12:29:10

标签: html forms node.js

var fs = require('fs'); // require file system module
var http = require('http'); // require http module

http.createServer(function(request,response){
var newFile = fs.createWriteStream("readme_copy.txt");
request.pipe(newFile);

request.on('end',function(){response.end('uploaded')});

}).listen(8888);

我正在尝试使用这段代码。我正在将文件上传到节点服务器,我正在创建上传文件的新副本。

我在下面提到了我的HTML文件。它正在点击服务器并上传文件,但文件副本为空。

如果我在命令提示符下使用curl上传同一个文件,它可以正常工作。

知道我错过了什么吗?

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-8">
</head>
<body>

<form action="http://localhost:8888/">
<input type="file" name="readme.txt" method="post" enctype="multipart/form-data">
<input type="submit">
</form>

<p><strong>Note:</strong> The accept attribute of the input tag is not supported in    Internet Explorer 9 and earlier versions.</p>
<p><strong>Note:</strong> Because of security issues, this example will not allow you to upload files.</p>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

您的Javascript缺少写入您通过调用createWriteStream创建的readme_copy文件的实际部分。

以下是您需要做的一个示例:

http://www.html5rocks.com/en/tutorials/file/filesystem/

相关问题