我堅持這個問題:我不能讓我的上傳工作:上傳從DART客戶端文件到節點服務器
這是一個Node.js的代碼taht與標準<form><input type="file" name="toUpload/>
router.post('/sp/file', function (req, res) {
// File to be uploaded
console.log("###" + req.files);
var fileToUpload = req.files.toUpload;
//console.log(fileToUpload);
var dir = __dirname + "/files";
/* var dir = __dirname + "/files/" + Date.now();
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}*/
fileToUpload.mv(__dirname + "/files/" + fileToUpload.name, function (err) {
if (err) {
console.log("error: " + err);
} else
console.log("upload succeeded");
console.log(fileToUpload);
console.log(__dirname + "/files/" + fileToUpload.name);
uploadFilesStorj.uploadFile(__dirname + "/files/" + fileToUpload.name);
});
});
現在,當我嘗試通過上傳鏢文件,我卡住,因爲發送的數據是不相同的格式:
class AppComponent {
void uploadFiles(dynamic files) {
if (files.length == 1) {
final file = files[0];
final reader = new FileReader();
//reader.onProgress.listen()
reader.onLoad.listen((e) {
sendData(reader.result);
});
reader.readAsDataUrl(file);
}
}
sendData(dynamic data) async {
final req = new HttpRequest();
req.onReadyStateChange.listen((Event e) {
if (req.readyState == HttpRequest.DONE &&
(req.status == 200 || req.status == 0)) {}
});
req.onProgress.listen((ProgressEvent prog) {
if (prog.lengthComputable)
print("advancement : " + (prog.total/prog.loaded).toString());
else
print("unable to compute advancement");
});
req.open("POST", "/sp/file");
req.send(data);
}
}
這是我的鏢角前方代碼
<input type="file" #upload (change)="uploadFiles(upload.files)"
(dragenter)="upload.style.setProperty('border', '3px solid green')"
(drop)="upload.style.setProperty('border', '2px dotted gray')" class="uploadDropZone" name="toUpload"/>
用這種方法發送的數據的格式爲: 請求有效載荷:
數據:text/html的; BASE64,PGh0bWw + DQogICA8aGVhZD4NCiAgICAgIDx0aXRsZT5GaWxlIFVwbG9hZGluZyBGb3JtPC9
我通過了很多時間沒有成功,任何人都可以幫助
我試圖直接加載數據中的node.js 應用.js: var rawBodySaver = function(req,res,buf,encoding)if(buf && buf.length){ \t req.rawBody = buf.toString(encoding ||'utf8'); } }; and in my upload.js var out = fs.createWriteStream(__ dirname +「/ files/test」); out.write(req.rawBody); 012.ec(); 事情是,我得到的原始數據編碼在BAS64前加上信息'data:text/html; base64' –