让storageVolume在JavaScript中工作

时间:2010-10-25 16:58:23

标签: air

在他的blog post中,Christian Cantrell展示了如何在ActionScript中使用STORAGE_VOLUME_MOUNT。

他编写了一个名为FileTile的Flex应用程序。

我希望看到一个JavaScript警告框,显示“您已插入”+ e.storageVolume.name,以及“您已删除存储卷”。

1 个答案:

答案 0 :(得分:0)

<html>
<head>
<title>New Adobe AIR Project</title>
<script type="text/javascript" src="lib/air/AIRAliases.js"></script>
<script type="text/javascript" src="lib/air/AIRIntrospector.js"></script>
<script type="text/javascript" src="lib/jQuery/jquery-1.4.3.min.js"></script>
<script type="text/javascript">
var msg = '<h1>Please insert your SwatchDog (SD) card.</h1>';

function trace() {
    var message = 'Hello Max';
    air.Introspector.Console.log(message);
}

function readFile(v) {
    var myFile = air.File.desktopDirectory.resolvePath("MyFile.txt");
    var fileStream = new air.FileStream();
    fileStream.open(myFile, air.FileMode.READ);
    $('#app').append('<p>' + fileStream.readUTFBytes(fileStream.bytesAvailable) + '</p>');
    fileStream.close();
}

function onVolumeMount(e) {
    $('#app').html('<h1>Thank you</h1><p>I can see you have multiple devices:</p>');
    var volumes = air.StorageVolumeInfo.storageVolumeInfo.getStorageVolumes();
    for (var i = 0; i < volumes.length; i++) {
        $('#app').append(volumes[i].rootDirectory.nativePath);
    }

    if (e.storageVolume.isRemovable) {
        if (e.storageVolume.name == 'SWATCHDOG') {
        $('#app').append('<p>And SwatchDog is drive: ' + e.storageVolume.rootDirectory.nativePath + '</p>');
        readFile(e.storageVolume)
      } else {
        $('#app').append('<p>But the one you just plugged in is not SwatchDog.</p>');
      }
    }
}

function onVolumeUnmount(e) {
    $('#app').html('<h1>Goodbye!</h1>' + msg);
}


jQuery(function($){
    var PluggedIn = false;
    var volumes = air.StorageVolumeInfo.storageVolumeInfo.getStorageVolumes();
    for (var i = 0; i < volumes.length; i++) {
        if (volumes[i].isRemovable) {
            PluggedIn = true;
            if (volumes[i].name == 'SWATCHDOG') {
                $('#app').append('I see you already have SwatchDog plugged in!');
                readFile(volumes[i])
            } else {
                $('#app').append('What you have plugged in is not SwatchDog.');
            }
        }
    }
    if (!PluggedIn){
        $('#app').append(msg);
    }
    air.StorageVolumeInfo.storageVolumeInfo.addEventListener(air.StorageVolumeChangeEvent.STORAGE_VOLUME_MOUNT, onVolumeMount);
    air.StorageVolumeInfo.storageVolumeInfo.addEventListener(air.StorageVolumeChangeEvent.STORAGE_VOLUME_UNMOUNT, onVolumeUnmount);
})
</script>
</head>
<body>
    <div id="app">
    </div>
    <button onclick="trace();">Say Hello Max!</button>
</body>
</html>