在javascript中使用文件选择器从Gdrive中选择文件时无法获取文件名

时间:2016-02-05 09:44:48

标签: javascript jquery

我正在尝试使用filepicker.js和javascript中的以下方法从Gdrive中选择一个文件,但是获取file.title是未定义的。如何获取所选文件名?我需要从所选文件中读取数据。 请帮我。     var picker = new FilePicker({

    apiKey:'XXXXXXXXXXX',
    clientId:'XXXXXXXXXXXXXXXXX',       
    buttonEl: document.getElementById('pick'),
    onSelect: function(file) {
        setTimeout("window.close()", 5000);
        fname=file.title;
        alert(fname)
        document.getElementById('filename').value=fname; 


    }

1 个答案:

答案 0 :(得分:0)

这可能是因为在获取文件名之前关闭窗口。试试这个可能对你有帮助。

<script>
    function initPicker() {
        var picker = new FilePicker({
            apiKey: 'PUT_YOUR_API_KEY_HERE',
            clientId: PUT_YOUR_NUMERIC_CLIENT_ID_HERE,
            buttonEl: document.getElementById('pick'),
            onSelect: function(file) {
                fname=file.title;
                alert(fname)
                document.getElementById('filename').value=fname; 
                setTimeout("window.close()", 5000);
            }
        }); 
    }
</script>