2014-10-30 46 views
0

我希望你能幫助我,我有一個問題。 我需要一個GAS表格,可以上傳文件到谷歌驅動器的原始名稱。但我不能工作。我不知道這是錯的,但該文件沒有擴展名和名稱是未定義的。 這是我的簡單代碼; 謝謝大家。GAS中的文件上傳同名和擴展名

function doGet(p) { 

    var app = UiApp.createApplication(); 
    var flow = app.createFlowPanel().setId('flow'); 

    var gridfile = app.createGrid(5,3); 
    var flabel0 = app.createLabel('Upload the file'); 
    var flabel1 = app.createLabel('Select file: '); 

    var thefile = app.createFileUpload().setName('thefile').setId('thefile'); 

    var handlerxx = app.createServerHandler('uploadfile').addCallbackElement(flow); 
thefile.addChangeHandler(handlerxx); 

    gridfile.setWidget(0, 0, flabel0) 
    .setWidget(2, 0, flabel1) 
    .setWidget(2, 1, thefile); 

    flow.add(gridfile); 

    app.add(flow); 
    return app; 
} 

function uploadfile(e) { 
    var app = UiApp.getActiveApplication(); 

    var fileBlob = Utilities.newBlob(e.parameter.thefile,"application/zip",e.parameter.thefile); 
    var doc = DocsList.createFile(fileBlob); 

    app.getElementById('flow').add(app.createLabel('File Uploaded successfully')); 
    return app; 
} 

回答

1

您只能上傳doGet/doPost表單結構中的文件。

而是詳細解釋我認爲這將是更容易表現出工作示例(但說實話,也更簡單...)

所以它是,請注意,我不得不添加一個提交按鈕來觸發表格提交。

順便說一句,我添加了一個'loading'由客戶端處理程序顯示的標籤,否則在上傳過程中沒有任何反應,用戶可能會擔心!

因爲上傳的文件會自動保留名稱和類型,所以我註釋了關於zip類型和fileName的一行。

function doGet() { 
    var app = UiApp.createApplication(); 
    var form = app.createFormPanel(); 
    var flow = app.createFlowPanel().setId('flow'); 
    form.add(flow); 
    var gridfile = app.createGrid(5,3); 
    var flabel1 = app.createLabel('Select file: '); 
    var lab = app.createLabel('LOADING').setStyleAttributes({'color':'red'}).setVisible(false).setId('lab'); 
var cliHandler = app.createClientHandler().forTargets(lab).setVisible(true); 
    var thefile = app.createFileUpload().setName('thefile').setId('thefile'); 
    var button = app.createSubmitButton('Upload the file').addClickHandler(cliHandler); 
    gridfile.setWidget(2, 0, flabel1) 
    .setWidget(2, 1, thefile) 
    .setWidget(2, 2, button); 
    flow.add(gridfile).add(lab); 

    app.add(form); 
    return app; 
} 

function doPost(e) { 
    var app = UiApp.getActiveApplication(); 
    Logger.log('doPost'); 
// var fileBlob = Utilities.newBlob(e.parameter.thefile,"application/zip",e.parameter.thefile); 
    var doc = DocsList.createFile(e.parameter.thefile); 
    app.getElementById('lab').setVisible(false); 
    app.add(app.createLabel('File Uploaded successfully')); 
    return app; 
} 
+0

謝謝大家,這個工作。我的應用程序不使用doPost(),但我會嘗試適應。 – 2014-10-31 14:26:08