这是我的 JavaScript:
<script>
//Make sure DOM is ready before mucking around.
$(document).ready(function()
{
console.log('DOM is ready!');
var socket = io.connect('http://localhost:8080');
//Each document field will have the following identifiers:
//classCode, description, professor, file
function uploadForm()
{
console.log('Creating FileReader object...');
var reader = new FileReader();
reader.onload = function(evt)
{
console.log('Read complete.');
console.log('Creating JSON object...')
var documentData =
{
'classCode':$('#classCode').val(),
'professor':$('#professor').val(),
'description':$('#description').val(),
'file':
{
'data': evt.target.result,
'metadata': document.getElementById('file').files[0]
},
'dateUploaded':new Date(),
'rating':0
};
console.log(documentData);
console.log('Adding document.');
socket.emit('addDocument', documentData);
};
console.log('Reading as binary string...');
reader.readAsDataURL(document.getElementById('file').files[0]);
};
});
</script>
我的 HTML 表单:
<form onsubmit = 'uploadForm()'>
<input type = 'text' placeholder = 'Class code' id = 'classCode' required/>
<input type = 'text' placeholder = 'Document description' id = 'description' required/>
<input type = 'text' placeholder = 'Professor' id = 'professor' required/>
<input type = 'file' id = 'file' required/>
<input type = 'submit' value = 'Upload!'/>
</form>
uploadForm()
当我通过元素上的.click
事件调用之前,此代码正在工作<input type='submit'>
,但这会忽略<form>
对元素的必需属性检查<input>
。所以现在我试图调用uploadForm()
的提交事件<form>
,但它运行不正常。reader.onload
事件应该在reader.readDataAsURL()
完成后被调用,但事实并非如此。我已经尝试过 jQuery.submit()
无济于事。
编辑:终于用我的手机记录了错误。Uncaught ReferenceError: uploadForm is not defined