google.script.run和form with input type = file产生字符串“FileUpload”而不是blob

时间:2013-08-17 19:05:32

标签: google-apps-script blob file-io

我使用带有gogs形式的google.script.run调用的HTMLServices,我尝试从我的表单输入文件字段中获取图像并将其存储在我的驱动器中。

除了photodata字段外,我正确获取所有字段的值。 photodata包含字符串“FileUpload”而不是blob。

我已经使用.getBlob测试了相同的结果。

服务器端:

function setUsager(form) {
  var blob = form.photodata;
  Logger.log(Utilities.jsonStingify(blob);
  DriveApp.getFolderById('0B2DdOMvW2Q84N0s4a21LM05wbms').createFile(form.usagerId,blob);
  obj = new {};
  obj.photo = file.getUrl();
  return obj;
}

单击“处理程序”

  function onUsagerEditFormSubmit() {
    loading(true);
    var form = this.parentNode;
    google.script.run.withSuccessHandler(showUsager)
        .withFailureHandler(showError)
        .setUsager(form);      

  }

形式:

<form name="usager-edit" id="usager-edit" method='post' enctype="multipart/form-data">
  <div class="hidden">
    <input type="text" name="type" id="type" value="USAGER" readonly="readonly" />
    <input type="text" name="usagerId" id="usagerId" readonly="readonly" />
  </div>
  <div class="field-container">      
    <label for="nom">Nom</label>
    <input type="text" name="nom" id="nom">
  </div>
  <div class="field-container">
    <label for="prenom">Prenom</label>
    <input type="text" name="prenom" id="prenom">
  </div>
  <div class="field-container">
    <label for="photo">Photo</label>
    <input class="file" type="file" name="photodata" id="photodata" accept='image/*'>
    <div id="prev-photo" name="prev-photo"></div>
    <div class="clear"></div>
  </div>
  <input class="myButton" onClick="onUsagerEditFormSubmit" type="button" name="save" id="save-button" value="Sauvegarder" />
</form>

1 个答案:

答案 0 :(得分:1)

您正在使用错误的createFile方法重载。

setUsager

当您使用createFile(name, content)时,您正在使用createFile(blob)

...
var file = DriveApp.getFolderById('****************************').createFile(blob);
...

您必须定义file才能制作:obj.photo = file.getUrl();

您还可以更改以下内容:

...
<input class="myButton" onClick="onUsagerEditFormSubmit" type="button" name="save" id="save-button" value="Sauvegarder" />
...

由:

...
<input class="myButton" onClick="onUsagerEditFormSubmit(this.parentNode)" type="button" name="save" id="save-button" value="Sauvegarder" />
...

  function onUsagerEditFormSubmit() {
    loading(true);
    var form = this.parentNode;
    google.script.run.withSuccessHandler(showUsager)
        .withFailureHandler(showError)
        .setUsager(form);    
  }

由:

  function onUsagerEditFormSubmit(form) {
    loading(true);
    google.script.run.withSuccessHandler(showUsager)
        .withFailureHandler(showError)
        .setUsager(form);    
  }

轻微变化:

...
Logger.log(Utilities.jsonStingify(blob);
...
obj = new {};
...

由:

...
Logger.log(Utilities.jsonStringify(blob));
...
var obj = {};
...