【发布时间】:2015-07-26 11:41:24
【问题描述】:
我正在使用 RecordRTC.js 在 chrome 中录制视频,然后使用 enctype='multipart/form-data' 将生成的 blob 发送到 node.js 服务器。
当我通过将 blob 转换为 dataURL 来发送发布请求时,
------WebKitFormBoundaryZMbygbTah7gTAgUa
Content-Disposition: form-data; name="name"
1encof615fpyoj85bzwo.webm
------WebKitFormBoundaryZMbygbTah7gTAgUa
Content-Disposition: form-data; name="type"
video/webm
------WebKitFormBoundaryZMbygbTah7gTAgUa
Content-Disposition: form-data; name="contents"
data:video/webm;base64,GkXfo0AgQoaBAUL3gQFC8oEEQvOBCEKCQAR3ZWJtQoeBA...
------WebKitFormBoundaryZMbygbTah7gTAgUa--
它工作正常。除非数据很大,否则视频无法正确保存。可能是因为尺寸过大,整个内容可能无法传输。
因此,我尝试将 blob 作为输入类型文件发送,但保存的视频似乎已损坏,因为它无法播放。
发送的 blob 在服务器上打印时是这样的:
Eߣ@ B��B��B��B�B�@webmB��B��S�g�fI�f@(*ױ@B@M�@whammyWA@whammyD�@žT�k@5�@2ׁsŁ��"�...
服务器端代码为:
function upload(response, file) {
var fileRootName = file.name.split('.').shift(),
fileExtension = file.name.split('.').pop(),
filePathBase = upload_dir + '/',
fileRootNameWithBase = filePathBase + fileRootName,
filePath = fileRootNameWithBase + '.' + fileExtension,
fileID = 2,
fileBuffer;
while (fs.existsSync(filePath)) {
filePath = fileRootNameWithBase + '(' + fileID + ').' + fileExtension;
fileID += 1;
}
file.contents = file.contents.split(',').pop(); // removed this when sent contents as blob
fileBuffer = new Buffer(file.contents, "base64");
fs.writeFileSync(filePath, fileBuffer);
}
我错过了什么?如何在文件上写入 blob 内容以便正确保存为 webm 文件?
【问题讨论】:
标签: node.js blob multipartform-data webm data-url