Skip to content

搞英语 → 看世界

翻译英文优质信息和名人推特

Menu
  • 首页
  • 独立博客
  • 专业媒体
  • 名人推特
  • 邮件列表
  • 关于本站
  • Product Hunt
  • Visual Capitalist
  • Elon Musk
Menu

如何使用 Node.js、Express 和 Multer 将文件上传到 Google Drive

Posted on 2022-04-13

本分步指南介绍了如何使用 Node.js、Express 和 Multer 构建用于将文件上传到 Google Drive 的 Web 表单。

Web 表单将文件编码为 multipart/form-data 并在POST请求中将数据发送到 Node.js 应用程序。 Multer 是一个用于处理多部分表单数据的 Express 中间件。

1. 创建 HTML 表单

HTML 表单包含允许上传多个文件的文件上传字段。它还包括受访者姓名、电子邮件和国家/地区的文本字段。

提交表单时,它使用浏览器的内置文件 API 将文件发送到 Node.js 应用程序。

 <! DOCTYPE html > < html > < head > < meta charset = " utf-8 " /> < meta name = " viewport " content = " width=device-width, initial-scale=1 " /> </ head > < body > < form > < input type = " file " name = " Files " required multiple /> < input type = " text " name = " Name " placeholder = " Name " /> < input type = " email " name = " Email Address " placeholder = " Email " required /> < input type = " text " name = " Country " placeholder = " Country " /> < button type = " submit " > Submit </ button > </ form > </ body > < script > const formElem = document . querySelector ( 'form' ) ; formElem . addEventListener ( 'submit' , async ( e ) => { e . preventDefault ( ) ; await fetch ( '/upload' , { method : 'POST' , body : new FormData ( formElem ) , } ) ; } ) ; </ script > </ html >

2. 创建 Node.js 应用程序

Node.js 应用程序将从表单接收文件并将它们上传到 Google Drive。主路由将呈现包含表单的 HTML 页面。

 // index.js const express = require ( 'express' ) ; const uploadRouter = require ( './router' ) ; const app = express ( ) ; app . get ( '/' , ( _ , res ) => { res . sendFile ( ` ${ __dirname } /index.html ` ) ; } ) ; app . use ( express . json ( ) ) ; app . use ( express . urlencoded ( { extended : true } ) ) ; app . use ( uploadRouter ) ; app . listen ( 8080 , ( ) => { console . log ( 'Form running on port 8080' ) ; } ) ;

3.谷歌驱动上传路由器

Multer 将 body 对象和 files 对象添加到请求对象。 body 对象包含表单的文本字段,而 files 对象将包含通过表单上传的文件。

您可以使用服务帐户对 Google Drive 服务进行身份验证。在 Google 云端硬盘中创建一个新文件夹,将该文件夹与服务帐户的电子邮件地址共享,并将 DRIVE_FOLDER_ID 替换为该文件夹的 ID。

 // router.js const stream = require ( 'stream' ) ; const express = require ( 'express' ) ; const multer = require ( 'multer' ) ; const { google } = require ( 'googleapis' ) ; const uploadRouter = express . Router ( ) ; const upload = multer ( ) ; const uploadFile = async ( fileObject ) => { const bufferStream = new stream . PassThrough ( ) ; bufferStream . end ( fileObject . buffer ) ; const { data } = await google . drive ( { version : 'v3' } ) . files . create ( { media : { mimeType : fileObject . mimeType , body : bufferStream , } , requestBody : { name : fileObject . originalname , parents : [ 'DRIVE_FOLDER_ID' ] , } , fields : 'id,name' , } ) ; console . log ( ` Uploaded file ${ data . name } ${ data . id } ` ) ; } ; uploadRouter . post ( '/upload' , upload . any ( ) , async ( req , res ) => { try { const { body , files } = req ; for ( let f = 0 ; f < files . length ; f += 1 ) { await uploadFile ( files [ f ] ) ; } console . log ( body ) ; res . status ( 200 ) . send ( 'Form Submitted' ) ; } catch ( f ) { res . send ( f . message ) ; } } ) ; module . exports = uploadRouter ;

原文: https://www.labnol.org/google-drive-api-upload-220412

本站文章系自动翻译,站长会周期检查,如果有不当内容,请点此留言,非常感谢。
  • Bob Nystrom (1)
  • Joel on Software (1)
  • John Resig (1)
  • Matt Might's blog (3)
  • News Letter (165)
  • Philip Walton (1)
  • Pivotal (1)
  • Scott Hanselman's Blog (2)
  • Tom's blog (1)
  • 英文媒体 (32,975)
    • Ars Technica (2,141)
    • Daily Infographic (250)
    • Engadget (5,221)
    • Enonomist (77)
    • FlowingData (228)
    • Hacker News (773)
    • Hacker News Daily (282)
    • Hacker Noon (125)
    • Harvard Health (139)
    • KK – Cool Tools (183)
    • KK – Recomendo (233)
    • Make Use Of (158)
    • NASA Astronomy Picture (238)
    • Product Hunt (6,610)
    • Psyche (207)
    • Quanta Magazine (171)
    • Science current issue (445)
    • Sidebar (929)
    • Singularity HUB (252)
    • TechCrunch (8,204)
    • The Practical Developer (99)
    • The Verge (5,673)
    • Visual Capitalist (337)
  • 英文推特 (15,775)
    • Bill Gates (304)
    • Brett Winton (1,128)
    • Cathie Wood (269)
    • Durov's Channel (25)
    • Elon Musk (4,595)
    • GeekWire (2,672)
    • Hunter Walk (47)
    • Mark Gurman (953)
    • Naval (655)
    • Parag Agrawal (52)
    • Ray Dalio (921)
    • Riccardo Mori (15)
    • Steph Smith (1,960)
    • Tim Cook (158)
    • Vitalik Buterin (2,021)
  • 英文独立博客 (3,405)
    • A learning a day (295)
    • A Smart Bear (2)
    • AddyOsmani.com (10)
    • Adwyat Krishna (29)
    • Ahmad Shadeed (2)
    • Alex Turek (1)
    • All Poetry (1)
    • All That is Solid (49)
    • André Staltz (2)
    • arxivblog (34)
    • Astral Codex Ten (15)
    • Atoms vs Bits (24)
    • AVC (35)
    • Basic Apple Guy (38)
    • Ben Thompson (13)
    • Benedict Evans (7)
    • Blog – storytelling with data (36)
    • Built For Mars (10)
    • Caleb Porzio (1)
    • Cameron Sun (1)
    • Christian Heilmann (33)
    • Christopher C (3)
    • Chun Tian (binghe) (1)
    • Codrops (13)
    • Cold Takes (14)
    • Dan Luu (1)
    • Daniel Lemire's blog (44)
    • David Amos (19)
    • David Perell (6)
    • David Walsh Blog (33)
    • Derek Sivers (23)
    • Desvl (13)
    • Devon's Site (5)
    • Digital Inspiration (26)
    • DKB Blog (4)
    • Douglas Vaghetti (12)
    • dropsafe (52)
    • DSHR (36)
    • Dunk (5)
    • DYNOMIGHT (36)
    • eagereyes (5)
    • Endless Metrics (135)
    • Entitled Opinions (8)
    • Exception Not Found (5)
    • Experimental History (18)
    • Farnam Street (5)
    • Fed Guy (10)
    • Felix Krause (3)
    • Florent Crivello (2)
    • Free Mind (6)
    • Full Stack Economics (40)
    • Funny JS (2)
    • Future A16Z (47)
    • Glassnode Insights (49)
    • Human Who Codes (3)
    • Infographics – Cool Infographics (10)
    • Information is Beautiful (10)
    • Irrational Exuberance (35)
    • Jacob Kaplan-Moss (13)
    • Jakob Greenfeld (40)
    • James Sinclair (3)
    • Jason Fried (17)
    • Jeff Kaufman (161)
    • John's internet house (16)
    • Johnny Rodgers (4)
    • Julia Evans (19)
    • Julian.com (2)
    • Kalzumeus (1)
    • Kevin Cox (10)
    • Kevin Norman (3)
    • KK – The Technium (50)
    • Krishna (6)
    • Lee Robinson (5)
    • Lines and Colors (42)
    • Lyn Alden – Investment Strategy (3)
    • Martin Fowler (22)
    • More To That (11)
    • Morgan Housel (69)
    • My Super Secret Diary (28)
    • Naval Blog (2)
    • Neckar's New Money (70)
    • Nick Whitaker (4)
    • Nicky's New Shtuff (1)
    • nutcroft (8)
    • Paul Graham (2)
    • Paul Graham: Essays (2)
    • Penguin Random House (52)
    • Phoenix's island (1)
    • Prof Galloway (31)
    • Python Weekly (25)
    • Rachel (32)
    • Real Life (34)
    • Sasha (47)
    • Science & technology (114)
    • Sébastien Dubois (6)
    • Secretum Secretorum (11)
    • Seth's Blog (117)
    • Shu Ding (3)
    • SignalFire (8)
    • Simon Willison's Weblog (120)
    • Simons Foundation (78)
    • SLIME MOLD TIME MOLD (20)
    • Slyar Home (8)
    • Spencer Greenberg (10)
    • Stay SaaSy (8)
    • Stephen Malina (4)
    • Stephen Wolfram Writings (2)
    • Strange Loop Canon (20)
    • Stratechery (9)
    • Tech Notes (10)
    • The Commonplace (29)
    • The Generalist (2)
    • The Intrinsic Perspective (28)
    • The Latest in Hearing Health | HeardThat (7)
    • The Mad Ned Memo (2)
    • The Rabbit Hole (31)
    • TLDR Newsletter (81)
    • Tomasz Tunguz (86)
    • Tony Kulesa (2)
    • Troy Hunt (50)
    • Tychlog (1)
    • Uncharted Territories (48)
    • Visualising Data (9)
    • Weichen Liu (20)
    • What's New (51)
    • Works in Progress (1)
    • Workspaces (32)
    • Writing (7)
    • Xe's Blog (30)
    • xkcd.com (105)
    • Yihui Xie (11)
    • Zoran Jambor (11)
©2023 搞英语 → 看世界 | Design: Newspaperly WordPress Theme