火狐3.6,顯然(夜間可能的Webkit)最新的Safari瀏覽器通過HTML5支持這一點。我最近一直在玩它,它工作得很好。我所做的示例只是在頁面中插入縮略圖,但可以調整此數據以將數據上載到服務器。這是我寫的的JavaScript/jQuery代碼,隨意使用這一點:
function debug(string) {
$("#debugArea").append(string);
}
$(function() {
// We need to override the dragover event, otherwise Firefox will just open the file on drop
$("#dropArea").bind("dragover", function(event) {
event.preventDefault();
});
// This is where the actual magic happens :)
$("#dropArea").bind("drop", function(event) {
event.preventDefault();
debug("Dropped something: ");
// Since jquery returns its own event object, we need to get the original one in order to access the files
var files = event.originalEvent.dataTransfer.files;
// jquery nicely loops for us over the files
$(files).each(function(index, file) {
if(!file.type.match(/image.*/)) { // Skip non-images
debug(file.name)
debug(" <em>not an image, skipping</em>; ");
return;
}
// We need a new filereader for every file, otherwise the reading might be canceled by the next file
var filereader = new FileReader();
filereader.onloadend = function(event) { $("#thumbnails").append("<img src='"+event.target.result+"' width='100px' />"); };
debug(file.name);
debug("; ");
// Read the file in data URL format so that we can easily add it to an img tag.
filereader.readAsDataURL(file);
});
debug("<br/><br/>");
})
});
併爲它的HTML:
<div id="dropArea">
<p>Drop images here!</p>
</div>
<div id="thumbnails">
</div>
<div id="debugArea">
<strong>Debug Info:</strong><br/>
</div>
我確認3年後,爲了設計一個Flash上傳器後備,似乎我們仍然需要使用AIR來拖放文件拖放功能。我搜索了沒有運氣的AS3解決方案。希望在2013年開始的時候,HTML5會變得更加普遍。 – 2012-12-21 21:24:20