【问题标题】:How to get path from html5 input field file object for phonegap如何从 html5 输入字段文件对象中获取 phonegap 的路径
【发布时间】:2013-07-10 11:23:51
【问题描述】:

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

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

问题是文件对象属性 .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)。请帮忙。

【问题讨论】:

    标签: javascript html file file-io cordova


    【解决方案1】:

    您无法从文件输入中获取路径数据。文件输入是只读的。尝试改变方法。使用 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>
    

    【讨论】:

    • 我尝试运行上面的代码,它在 weinre 或 phonegap-desktop 的控制台日志中冻结而没有错误...
    • 好的,我修复了代码中的所有错误,但是当我尝试运行它时,它会抛出错误代码 5。
    • 错误代码 5 可能意味着路径有问题。要正确设置路径,请参阅以下帖子,stackoverflow.com/questions/6417055/…
    • 我明白了,它不需要路径,只需要一个文件名,路径会根据平台自动填充,slick!太感谢了。更新了更正拼写错误的答案。
    猜你喜欢
    • 2014-07-26
    • 2017-03-26
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    相关资源
    最近更新 更多