DataTransferItem.getAsFile()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
アイテムがファイルの場合、 DataTransferItem.getAsFile()
メソッドはドラッグデータ項目の File
オブジェクトを返します。項目がファイルでない場合、このメソッドは null
を返します。
構文
js
getAsFile()
引数
なし。
返値
例
この例が、 getAsFile()
メソッドを drop
イベントハンドラー内で使用するものです。
js
function drop_handler(ev) {
console.log("Drop");
ev.preventDefault();
const data = ev.dataTransfer.items;
for (let i = 0; i < data.length; i += 1) {
if (data[i].kind === "string" && data[i].type.match("^text/plain")) {
// This item is the target node
data[i].getAsString((s) => {
ev.target.appendChild(document.getElementById(s));
});
} else if (data[i].kind === "string" && data[i].type.match("^text/html")) {
// Drag data item is HTML
console.log("… Drop: HTML");
} else if (
data[i].kind === "string" &&
data[i].type.match("^text/uri-list")
) {
// Drag data item is URI
console.log("… Drop: URI");
} else if (data[i].kind === "file" && data[i].type.match("^image/")) {
// Drag data item is an image file
const f = data[i].getAsFile();
console.log("… Drop: File ");
}
}
}
仕様書
Specification |
---|
HTML # dom-datatransferitem-getasfile-dev |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
getAsFile |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.