匿名Google云端硬盘上传创建新文件夹

时间:2017-08-24 14:16:45

标签: javascript file-upload google-apps-script

我正在使用here找到的Google Apps脚本让匿名用户将文件上传到我的Google云端硬盘。

我想要的是脚本使用表单上的名称输入创建文件夹,然后将文件存放在该文件夹中。

到目前为止,我以某种方式工作(我想我应该提到我不知道我在做什么)。

我还希望脚本检查文件夹是否已经存在,然后将新文件存入文件夹。

示例:Bobby将file1上传到文件夹'Bobby'。然后他回来并上传file2。 File2也应该保存在'Bobby'文件夹中。

现在,脚本正在检查文件夹名称,当它看到它已经创建时,它会将文件存放在根文件夹“Uploads”中,而不是文件夹“Bobby”。

如何让脚本在用户现有文件夹中存放来自同一用户的其他文件,而无需创建新文件夹而无需保存到根文件夹?

以下是我现在正在使用的内容:(我提到我不知道我在做什么吗?)

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('form.html');

}

function uploadFiles(form) {

  try {

    var upload = "Uploads";
    var folder, folders = DriveApp.getFoldersByName(upload);

    if (folders.hasNext()) {
      folder = folders.next();
    } else {
      folder = DriveApp.createFolder(upload);
    }

    var blob = form.file;
    var name = form.name;

    var filefolder, filefolders = DriveApp.getFoldersByName(name);

    if (filefolders.hasNext()) {
      filefolder = filefolders.next();
      var file = folder.createFile(blob);
      file.setDescription("Uploaded by " + name);
    } else {
      var file = folder.createFolder(name).createFile(blob);
      file.setDescription("Uploaded by " + name);
    }

    return "File uploaded successfully " + file.getUrl();

  } catch (error) {

    return error.toString();
  }

}
<!doctype html>
<style type="text/css">
  * {
    font-family: "Verdana";
  }
  
  body {
    background-color: #EEEEEE;
  }
  
  .container {
    margin: 0 auto;
    display: block;
    width: 50%;
  }
  
  #myForm {
    width: 50%;
    margin: 0 auto;
    background: #f4d03f;
    border-radius: 10px;
    box-shadow: 5px 5px 10px #aaa;
    padding: 20px;
    color: #fff;
  }
  
  .inputname {
    display: inline-block;
    height: 30px;
    width: 300px;
    padding: 5px;
    border: none;
    text-align: center;
    background-color: #eee;
  }
  
  .inputname:hover {
    background: #fff;
    box-shadow: 0 0 15px #777;
    transition: 0.2s;
  }
  
  .inputfile {
    width: 0.1px;
    height: 0.1px;
    opacity: 0;
    overflow: hidden;
    position: absolute;
    z-index: -1;
  }
  
  .inputfile+label,
  .inputsubmit {
    color: #fff;
    font-size: 1.25em;
    font-weight: 700;
    display: inline-block;
    background-color: #2abb9b;
    padding: 20px;
    cursor: pointer;
    border: none;
    border-radius: 3px;
  }
  
  .inputfile:focus+label,
  .inputfile+label:hover,
  .inputsubmit:hover {
    background-color: #87D37C;
    box-shadow: 0 0 15px #777;
    transition: 0.2s;
  }
</style>

<div class="container" align="center">
  <form id="myForm" align="center">
    <h1>Teacher Mike's Google Drive</h1>
    <h2>Upload Files Below</h2>
    <hr>
    <input type="text" class="inputname" name="name" placeholder="Your nickname..">
    <!--<input type="file" class="file" name="myFile">
            <label for="myFile">Choose a File</label>-->
    <input type="file" name="file" id="file" class="inputfile" />
    <label for="file">Choose a file</label>
    <input type="submit" class="inputsubmit" value="Upload File" onclick="this.value='Uploading..';
                    google.script.run.withSuccessHandler(fileUploaded)
                    .uploadFiles(this.parentNode);
                    return false;">
    <hr>
  </form>
  <div id="output"></div>
  <script>
    function fileUploaded(status) {
      document.getElementById('myForm').style.display = 'none';
      document.getElementById('output').innerHTML = status;
    }
  </script>
  <style>
    input {
      display: block;
      margin: 20px;
    }
  </style>
</div>

提前致谢!

P.S。文件成功上传后,是否有办法打开文件夹?

1 个答案:

答案 0 :(得分:1)

我认为当同一个用户上传文件时,保存文件的文件夹可能有误。请修改

来自:

if (filefolders.hasNext()) {
  filefolder = filefolders.next();
  var file = folder.createFile(blob);
  file.setDescription("Uploaded by " + name);
} else {
  var file = folder.createFolder(name).createFile(blob);
  file.setDescription("Uploaded by " + name);
}

致:

if (filefolders.hasNext()) {
  filefolder = filefolders.next();
  var file = filefolder.createFile(blob); // <--- Here
  file.setDescription("Uploaded by " + name);
} else {
  var file = folder.createFolder(name).createFile(blob);
  file.setDescription("Uploaded by " + name);
}

如果没有解决,我很抱歉。