Diễn đàn » PHP - HTML - CSS » Tải tệp lên Google Drive từ HTML bên ngoài không cần Authorization (Max file size 50mb)
Avatar
Dai I'm a cage, in search of a bird
Muốn chỉ tiết hơn thì đọc từ nguôn 23
Nguồn:https://tanaikech.github.io/2020/08/13/uploading-file-to-google-drive-from-external-html-without-authorization/

BƯỚC 1

Vàohttps://script.new/ tạo project mới với code bên dưới.
Lưu ý: thay folderId với thư mục muốn upload file vào.

function doPost(e) {
 const folderId = "###"; // Folder ID which is used for putting the file.

 const blob = Utilities.newBlob(
 JSON.parse(e.postData.contents),
 e.parameter.mimeType,
 e.parameter.filename
 );
 const file = DriveApp.getFolderById(folderId || "root").createFile(blob);
 const responseObj = {
 filename: file.getName(),
 fileId: file.getId(),
 fileUrl: file.getUrl(),
 };
 return ContentService.createTextOutput(
 JSON.stringify(responseObj)
 ).setMimeType(ContentService.MimeType.JSON);
}function doPost(e) {
 const folderId = "###"; // Folder ID which is used for putting the file.

 const blob = Utilities.newBlob(
 JSON.parse(e.postData.contents),
 e.parameter.mimeType,
 e.parameter.filename
 );
 const file = DriveApp.getFolderById(folderId || "root").createFile(blob);
 const responseObj = {
 filename: file.getName(),
 fileId: file.getId(),
 fileUrl: file.getUrl(),
 };
 return ContentService.createTextOutput(
 JSON.stringify(responseObj)
 ).setMimeType(ContentService.MimeType.JSON);
}


Deploy code trên và copy url của Web Apps
Dạng như thế này https://script.google.com/macros/s/###/exec

BƯỚC 2

HTML thay link https://script.google.com/macros/s/###/exec bằng url của Web Apps ở BƯỚC 1

GHI CHÚ
* Mở Console ở Develope Tools để xe kết quả.
* Có thể Upload lên Shared Drive.
* Kích thước tệp tối đa là 50 MB. Bởi vì trong giai đoạn hiện tại, kích thước blob tối đa là 50 MB tại Google Apps Script.

<form id="form">
 <input name="file" id="uploadfile" type="file" />
 <input id="submit" type="submit" />
</form>

<script>
 const form = document.getElementById("form");
 form.addEventListener("submit", (e) => {
 e.preventDefault();
 const file = form.file.files[0];
 const fr = new FileReader();
 fr.readAsArrayBuffer(file);
 fr.onload = (f) => {
 const url = "https://script.google.com/macros/s/###/exec"; // Please set the URL of Web Apps.

 const qs = new URLSearchParams({
 filename: file.name,
 mimeType: file.type,
 });
 fetch(`${url}?${qs}`, {
 method: "POST",
 body: JSON.stringify([...new Int8Array(f.target.result)]),
 })
 .then((res) => res.json())
 .then(console.log)
 .catch(console.log);
 };
 });
</script>
Gill, Thanh Tam đã thích bài viết này
Trống!
Trống!
  • BBcode:
  • Markdown: