Chrome:使用 jQuery 触发点击事件(适用于 Firefox)
这个想法是:你有一个隐藏的文件字段和一个图像。您使用该图像来显示选择文件对话框。然后我在画布中显示图像。
<input id="ytfile-select" type="hidden" value="" name="Foto[image]" />
<input style="display:none" id="file-select" accept="image/*"
name="Foto[image]" type="file" />
<img id="upload-image" src="/images/design/upload-image.png"
alt="upload-image-button" />
<canvas id="canvas">
Sorry, your browser doesn't support the <canvas> element.
</canvas>
$('#upload-image').click(function(){
$('#file-select').click();
});
$('#file-select').bind('change',function(){
var fileList = this.files;
var img = document.createElement("img");
img.classList.add("obj");
img.src = window.URL.createObjectURL(fileList[0]);
var ctx = document.getElementById('canvas').getContext("2d");
ctx.drawImage(img,0,0);
});
在 Firefox 10 上,ctx.drawImage(img,0,0);
仅当我在该行上有 Firebug 调试器和断点时才有效。如果没有断点,它就不起作用。我在另一个干净的个人资料上检查了它。
在 Chrome 上,$('#file-select').click();
不会打开文件对话框。
编辑:这个问题已经得到解答。但是,我不知道 Firefox 的问题是什么。有什么想法吗?
我使用这些网站来创建此代码:
- https://developer.mozilla.org/en/using_files_from_web_applications
- http://hacks.mozilla.org/2011/01/how-to-develop-a-html5-image-uploader/
编辑2: 我通过这样做解决了 Firefox 的问题: 在 var fileList = this.files;
行后面,我输入了:
reader = new FileReader();
reader.onload = function (event) {
$('#display').append('<img src ="' + event.target.result + '">');
};
reader.readAsDataURL(fileList[0]);
The idea is: you have a file field, which is hidden, and an image. You use the image to show the select file dialog. Then I display the image in the canvas.
<input id="ytfile-select" type="hidden" value="" name="Foto[image]" />
<input style="display:none" id="file-select" accept="image/*"
name="Foto[image]" type="file" />
<img id="upload-image" src="/images/design/upload-image.png"
alt="upload-image-button" />
<canvas id="canvas">
Sorry, your browser doesn't support the <canvas> element.
</canvas>
$('#upload-image').click(function(){
$('#file-select').click();
});
$('#file-select').bind('change',function(){
var fileList = this.files;
var img = document.createElement("img");
img.classList.add("obj");
img.src = window.URL.createObjectURL(fileList[0]);
var ctx = document.getElementById('canvas').getContext("2d");
ctx.drawImage(img,0,0);
});
On Firefox 10, ctx.drawImage(img,0,0);
only works when I have the Firebug debugger nad breakpoint on that line. Without the breakpoint, it doesn't work. I checked it on another clean profile.
On Chrome, $('#file-select').click();
doesn't open the file dialog.
Edit: this question has been already answered. However, I have no idea what the issue with Firefox is. Any ideas?
I used these websites to create this code:
- https://developer.mozilla.org/en/using_files_from_web_applications
- http://hacks.mozilla.org/2011/01/how-to-develop-a-html5-image-uploader/
Edit 2:
I resolved the issue with the Firefox by doing this:
Behind the line var fileList = this.files;
, I put:
reader = new FileReader();
reader.onload = function (event) {
$('#display').append('<img src ="' + event.target.result + '">');
};
reader.readAsDataURL(fileList[0]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将触发
click
事件:$('#file-select').trigger("click");
示例:
JSFiddle 演示: http://jsfiddle.net/t7P3v/
更新:
事实上,如果 <代码>输入type="file" 为
display:none
它不会弹出。您仍然可以通过一些 CSS 使其对用户不可见:position:absolute;顶部:-999px;左:-999px
This will trigger the
click
event:$('#file-select').trigger("click");
Example:
JSFiddle demo: http://jsfiddle.net/t7P3v/
Update:
Indeed, if the
input type="file"
isdisplay:none
it will not pop up. Still you can make it invisible to the user by some CSS:position:absolute; top:-999px; left:-999px
在 jsfiddle 上尝试此代码。我不记得在哪里复制了这个插件。
插件代码
使用
try this code on jsfiddle. I cant remember where I copy this plugin.
plugin code
usage