这里是我的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