我在前端使用Plupload實現文件上傳,並在後端使用multer中間件在後端使用multipart/form上傳來實現文件上傳。目前沒有可用的例子,所以這是我到目前爲止:如何處理expressjs中的plupload使用multer與組塊?
HTML前端:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Test</title>
</head>
<ul id="filelist"></ul>
<br />
<div id="container">
<a id="browse" href="javascript:;">[Browse...]</a>
<a id="start-upload" href="javascript:;">[Start Upload]</a>
<br />
<pre id="console"></pre>
</div>
<script src="/plupload/js/plupload.full.min.js"></script>
<script type="text/javascript">
var uploader = new plupload.Uploader({
browse_button: 'browse', // this can be an id of a DOM element or the DOM element itself
url: '/upload'
});
uploader.init();
uploader.bind('FilesAdded', function(up, files) {
var html = '';
plupload.each(files, function(file) {
html += '<li id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></li>';
});
document.getElementById('filelist').innerHTML += html;
});
uploader.bind('UploadProgress', function(up, file) {
document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
});
uploader.bind('Error', function(up, err) {
document.getElementById('console').innerHTML += "\nError #" + err.code + ": " + err.message;
});
document.getElementById('start-upload').onclick = function() {
uploader.start();
};
</script>
</html>
它基本上只是plupload快速入門指南:http://www.plupload.com/docs/v2/Getting-Started
後端使用節點明確。我修整我的代碼減少到最低限度工作版本,在這裏使用的SO:
const path = require('path');
const express = require('express');
var multer = require('multer');
var upload = multer({ dest: 'uploads/' });
// Create express
const app = express();
app.use(express.static('public'));
app.post('/upload', upload.array('file'), function(req, res){
console.log(req.files);
})
app.listen(3000, function() {
console.log('App running...');
});
基本上,只是一個普通的快遞應用與multer封裝,提供靜態文件。
問:
如何在前端和使用的NodeJS上傳Plupload文件在後臺(使用快遞,multer)?它也應該支持分塊。
HI大衛,任何想法如何讓快遞「交談」plupload? – kosinix
@kosinix是的,你可以很容易地做到這一點,檢查我上面的更新答案(在**更新**標題下) –