JavaScript中的onload函数不起作用Android
onload function in javascript not working Android
我正在尝试将图像读取为文件,然后显示图像名称,大小等。我调试了我的代码,发现我的 onload 函数正在加载但不起作用。谢谢你的帮助。我已经包含了我的整个FileSelection()方法。
var iBytesUploaded = 0;
var iBytesTotal = 0;
var iPreviousBytesLoaded = 0;
var iMaxFilesize = 1048576; // 1MB
var oTimer = 0;
var sResultFileSize = '';
var oImage = new Image();
function fileSelected() {
alert("hello");
// hide different warnings
document.getElementById('upload_response').style.display = 'none';
document.getElementById('error').style.display = 'none';
document.getElementById('error2').style.display = 'none';
document.getElementById('abort').style.display = 'none';
document.getElementById('warnsize').style.display = 'none';
// get selected file element
var oFile = document.getElementById('image_file').files[0];
// filter for image files
var rFilter = /^(image'/bmp|image'/gif|image'/jpeg|image'/png|image'/tiff)$/i;
if (! rFilter.test(oFile.type)) {
alert("problem");
document.getElementById('error').style.display = 'block';
return;
}
// little test for filesize
if (oFile.size > iMaxFilesize) {
document.getElementById('warnsize').style.display = 'block';
return;
}
// get preview element
oImage = document.getElementById('preview');
alert("hello2");
// prepare HTML5 FileReader
var oReader = new FileReader();
alert("hello3")
oReader.onload = function(e)
{
alert("hello4");
alert("reached");
oImage.onload = function () { // binding onload event
alert("hi");
// oImage.src = e.target.result;
// we are going to display some custom image information here
sResultFileSize = bytesToSize(oFile.size);
document.getElementById('fileinfo').style.display = 'block';
document.getElementById('filename').innerHTML = 'Name: ' + oFile.name;
document.getElementById('filesize').innerHTML = 'Size: ' + sResultFileSize;
document.getElementById('filetype').innerHTML = 'Type: ' + oFile.type;
document.getElementById('filedim').innerHTML = 'Dimension: ' + oImage.naturalWidth + ' x ' + oImage.naturalHeight;
};
// e.target.result contains the DataURL which we will use as a source of the image
oImage.src = e.target.result;
};
alert("file found");
// read selected file as DataURL
oReader.readAsDataURL(oFile);
}
给你我在评论中谈论的更改:
//You want this somewhere
var oImage = new Image();
var oReader = new FileReader();
alert("hello3")
oReader.onload = function(e)
{
alert("hello4");
oImage.onload = function () { // binding onload event
alert("hi");
// we are going to display some custom image information here
sResultFileSize = bytesToSize(oFile.size);
document.getElementById('fileinfo').style.display = 'block';
document.getElementById('filename').innerHTML = 'Name: ' + oFile.name;
document.getElementById('filesize').innerHTML = 'Size: ' + sResultFileSize;
document.getElementById('filetype').innerHTML = 'Type: ' + oFile.type;
document.getElementById('filedim').innerHTML = 'Dimension: ' + oImage.naturalWidth + ' x ' + oImage.naturalHeight;
};
// Make sure this comes after
oImage.src = e.target.result;
};
// read selected file as DataURL
oReader.readAsDataURL(oFile);
相关文章:
- 从Android设备浏览时Javascript不起作用
- Appcelerator Android Custom Fonts不起作用
- JavaScript Audio 对象在 Android API 19 中不起作用
- Phonegap (Android) - 数据库更改版本不起作用
- Phonegap构建:通知/弹出在android中不起作用
- 为什么除了数据库(WebSQL)之外,其他数据库名称在android中不起作用
- Phonegap地理定位在Android上不起作用
- 粘性/固定标头在 Firefox 和 Android 中不起作用
- HTML 5 代码在使用 PhoneGap 的 Android 上不起作用
- Cordova电子邮件作曲家在Android上不起作用
- jQuery Slider 在 Android Chrome 浏览器上不起作用
- 在 Web 视图上自动启动 html5 视频,在 Android 4.2.2 上不起作用
- Windows.Scroll事件在Android Web View上不起作用
- Cordova iframe在iOS Safari中工作,但在Android中不起作用
- HTML5 JavaScript 自动播放在 Android 4.4 上不起作用
- JavaScript Android不起作用
- Android 浏览器 .click() 不起作用 javascript
- PhoneGap 3.0.0 Tanelih 蓝牙插件在 Android 上已启用错误回调不起作用
- Javascript 在 WebView 中不起作用 - Android
- JavaScript中的onload函数不起作用Android