admin管理员组文章数量:1279147
You cannot re-select and upload the same file except in Firefox, which mistakenly allows you to do so:
<input type="file" id="fileChooser">
document.getElementById('fileChooser').onchange = function () {
alert('Uploaded!');
};
Here's my approach to resolve the issue. I wonder if there's a neater way to achieve it.
<input type="file" id="fileChooser">
var fileChooser = document.getElementById('fileChooser');
fileChooser.onclick = function () {
this.value = '';
};
fileChooser.onchange = function () {
if (this.value) {
alert('Uploaded!');
}
};
On JSFiddle: /
Explanation:
You cannot re-select the same file twice in a row, i.e. you select and upload foo.txt
on your desktop, for example, and then click the file chooser again, file select dialog appears and you try to select the same file again -- the browser simply does nothing and no alert box appears.
You cannot re-select and upload the same file except in Firefox, which mistakenly allows you to do so:
<input type="file" id="fileChooser">
document.getElementById('fileChooser').onchange = function () {
alert('Uploaded!');
};
Here's my approach to resolve the issue. I wonder if there's a neater way to achieve it.
<input type="file" id="fileChooser">
var fileChooser = document.getElementById('fileChooser');
fileChooser.onclick = function () {
this.value = '';
};
fileChooser.onchange = function () {
if (this.value) {
alert('Uploaded!');
}
};
On JSFiddle: http://jsfiddle/scMF6/2/
Explanation:
You cannot re-select the same file twice in a row, i.e. you select and upload foo.txt
on your desktop, for example, and then click the file chooser again, file select dialog appears and you try to select the same file again -- the browser simply does nothing and no alert box appears.
- 6 Mind explaining the issue you're trying to solve better? I have no idea what you're trying to achieve. – Fabrício Matté Commented Jun 8, 2014 at 5:25
-
2
Are you sure? I'm getting a change event on Chrome and IE as well. AFAIK file inputs'
.onchange
works pretty much on any browser. – Fabrício Matté Commented Jun 8, 2014 at 5:43 - 3 I need to explain more: you cannot reselect/reupload the same file, i.e. if you choose a file, you cannot choose it again -- it simply does nothing. – user648340 Commented Jun 8, 2014 at 5:48
- 2 In my opinion, "Upload the same file" implies that you're submitting/sending the same file twice. In your case, you're selecting it before submission. – Fabrício Matté Commented Jun 8, 2014 at 5:59
- 3 There is no question in this question. – AD7six Commented Jun 13, 2014 at 18:10
3 Answers
Reset to default 3Thanks for idea! If some one use GWT, then you can use this code)
...//some code :)
Event.setEventListener(btn, new EventListener() {
@Override
public void onBrowserEvent(Event event) {
//work with IE11+ and other modern browsers
nativeClearFile(fileUpload.getElement());
//throw event click
InputElement.as(fileUpload.getElement()).click();
}
});
Event.sinkEvents(btn, Event.ONCLICK);
...//continue code :)
private native void nativeClearFile(Element element) /*-{
element.value = '';
}-*/;
Your solution has one problem, after selecting a file when you click it second time it clears the file input. Now if user does not select a new file and cancels Browse pop up then his old selection is gone.
This is not the default behavior of file input in FF.
I guess if you have some handle or callback for upload then you should clear your file input in that.
you must initialize "change event" after "click event":
var fileChooser = document.getElementById('fileChooser');
fileChooser.onclick = function () {
this.value = '';
fileChooser.onchange = function () {
if (this.value) {
alert('Uploaded!');
}
};
};
本文标签: javascriptReselect and upload the same fileStack Overflow
版权声明:本文标题:javascript - Re-select and upload the same file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741300480a2371062.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论