【发布时间】:2011-10-05 05:15:49
【问题描述】:
我想在我的服务器上上传文件并根据它们的内容命名它们。这应该很简单(它在 python 中),但我很难弄清楚如何在 Node.js 中做到这一点。
我正在使用 express 和 connect-form,really just usesformidable。我还看到该节点有一个名为 crypto 的库,它与 python 的 hashlib 非常相似。现在我只需要了解如何流式传输连接表单给我的临时文件并对其进行哈希处理。
这是我想做的 Python/Flask(ish) 实现。
import hashlib
from Flask import request
def upload():
file = request.files['file']
hash = hashlib.sha256()
name, ext = file.filename.rsplit('.', 1)
try:
for chunk in file.chunks()
hash.update(chunk)
finally:
file.seek(0)
new_name = "%s.%s" % (hash.hexdigest(),ext)
file.save(os.path.join(UPLOAD_DIR, new_name))
我见过很多这样的toy answers that just print out the file's name,但没有一个真正读写数据。
【问题讨论】: