0
我在骨幹初學者所以請原諒我的愚蠢,我試圖通過添加Backbone.editors功能來上傳文件:在我的觀點,即是我寫的:文件中使用骨幹,XHR和上傳未發現節點JS
Backbone.Form.editors.Filepicker = Backbone.Form.editors.Base.extend(_.extend(
validatorExt, disablingExt, autoActionExt, {
className: 'form-data',
events: {
'change input[type=file]': 'uploadFile'
},
initialize: function (options) {
Backbone.Form.editors.Base.prototype.initialize.call(this,options);
this.$uploadInput = $(
'<input type="file" multiple="multiple" id="' + this.key +
'" name="' + this.key + '" multiple/>');
this.$loader = $(
'<p class="upload-status"><span class="loader"></span> Uploading…</p>'
);
this.$error = $('<p class="upload-error error">Error</p>');
this.$list = $('<ul class="file-list">');
},
uploadFile: function() {
var xhr = new XMLHttpRequest();
var url = 'upload/';
this.onProgress(0, 'Upload started.');
var files = document.getElementById(this.key)
.files;
var file = files[0];
xhr.addEventListener('progress', function (e) {
var done = e.position || e.loaded,
total = e.totalSize || e.total;
console.log('xhr progress: ' + (Math.floor(done/total *
1000)/10) + '%');
}, false);
if (xhr.upload) {
xhr.upload.onprogress = function (e) {
var done = e.position || e.loaded,
total = e.totalSize || e.total;
console.log('xhr.upload progress: ' + done + '/' +
total + ' = ' + (Math.floor(done/total * 1000)/
10) + '%');
};
}
xhr.onreadystatechange = function (e) {
if (4 === this.readyState) {
console.log(['xhr upload complete', e]);
}
};
xhr.open('POST', url, true);
console.log(xhr);
var formData = new FormData();
formData.append(this.key, file, file.name);
xhr.send(formData);
}));
在我的服務器端:
app.post('/upload/', multer({dest: './tmp/my-uploads'}).array(), uploadFile);
function uploadFile(req, res) {
console.log(req.body.file);
console.log(req.file);
res.status(204).end();
}
這個腳本創建上傳文件夾,但該文件沒有在服務器端轉移!雖然我可以讀取文檔的名稱和大小,而console.log都是未定義的! 我認爲真正的問題,web服務「/上傳/不發送二進制數據。我該怎麼辦?
它解決了!我曾與這行代碼來激活我的服務器上傳服務:
app.use(express.bodyParser({
keepExtensions: true,
uploadDir: '/uploads'
});
總是同樣的問題,我沒有找到該文件的二進制代碼,將其發送到服務器 – chahach
你檢查你的瀏覽器的Web開發工具的要求,以確保確實正在發送的文件中的字段(什麼字段名稱正在使用)? – mscdex
是,在views.js,cosole.log(文件)顯示文件,它的名稱,大小......和的FileReader(文件)顯示文件的內容 – chahach