1

如何从 html5 输入字段返回的文件对象中获取路径?

基本上,phonegap 应用程序设置为在线并从第三方网站下载声音文件,然后浏览到下载的文件并将其移动到本地目录。(短 < 1 秒的声音文件)。

问题是文件对象属性 .fullPath 未定义。getFile 接受路径输入,而不是 [object File] 输入。

从以下代码:

<input style="opacity:0;" type="file" name="soundInput" id="soundInput"/>

<script type="text/javascript" charset="utf-8">
    var soundInput = document.getElementById('soundInput');
    soundInput.addEventListener('change', function(){handleFileSelect(type);}, false);
    soundInput.click();

    function handleFileSelect() {
        var file = this.files[0]; // FileList object
        var type = isThumpy;

        alert("file = " + file.name + "\n full path = " + file.fullPath);

        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
            gotFS(fs,file,type);
        }, fail);
    }

    function fail(error) {
        alert("error " + error.code);
    }

    function gotFS(fileSystem, file, type) {
        alert("got filesystem");
        var flags = {create: false, exclusive: false};
        fileSystem.root.getFile(file, flags, function(fe){
            gotFileEntry(type,fe);
        },fail);
    }

    function gotFileEntry(type, fileEntry) {
        alert("got fileEntry");
        fileEntry.copyTo(path, function(fe){successfulCopy(type, fe);}, fail);
    }

    function successfulCopy(type, fileEntry) {
        alert("copy success");
        setSoundByUri(type, fileEntry.name)
    }
</script>

它不会越过“got filesystem”,也不会抛出错误(“error” + error.code)。请帮忙。

4

1 回答 1

3

您无法从文件输入中获取路径数据。文件输入是只读的。尝试改变方法。使用 phoneGap fileEntry 创建新文件并将文件输入中的数据写入其中。

像这样:

<script type="text/javascript" charset="utf-8">
function handleFileSelect(type) {
    var file = this.files[0]; // FileList object
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
        gotFS(fs,file,type);
    }, fail);

}

function gotFS(fileSystem, file, type) {
    var flags = {create: true, exclusive: false};
    fileSystem.root.getFile(file.name, flags, function(fe) {gotFileEntry(fe, file, type);}, fail);
}

function gotFileEntry(fileEntry, file, type) {
    fileEntry.createWriter(function(w){gotFileWriter(w, file, type);}, fail);
}

function gotFileWriter(fileWriter, file, type) {
    fileWriter.onwriteend = function(evt){setSoundByUri(type, path + file.name);};
    var reader = new FileReader();
    reader.onload = function(event) {
        var rawData = event.target.result;
        fileWriter.write(rawData);
    };
    reader.onerror = function(event){
        alert("error, file could not be read" + event.target.error.code);
    };
    reader.readAsArrayBuffer(file);
}

function fail(error) {
    alert("error " + error.code);
    if (error.code == FileError.PATH_EXISTS_ERR){
        alert("The file already exists.");
    }
}
</script>
于 2013-07-12T20:06:35.440 回答