Mat*_*t.G 6 javascript node.js google-drive-api google-api-js-client
我有一个 Chrome 扩展程序,它从后端 Node.js 服务器获取文件信息,以便在扩展程序中提供 google 驱动器文件。在这个特定实例中,我遇到的问题是将后端保存的文件上传到前端的gapi客户端,该客户端允许用户将其上传到他们的谷歌驱动器。身份验证一切都很好,主要问题是上传。
这是我的代码。
背景.js(客户端):
var parentFolder;
var fileArray;
let classroomUrl = 'https://connect.smartpathed.com/getclassroomparent';
await fetch(classroomUrl)
.then(response => response.text())
.then(data => parentFolder = data)
.then(data => console.log(parentFolder));
let fileArrayUrl = "https://connect.smartpathed.com/getclassroomarrayselect";
await fetch(fileArrayUrl)
.then(response => response.json())
.then(data => fileArray = data)
.then(data => console.log(fileArray));
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
gapi.client.init({
apiKey: API_KEY,
discoveryDocs: DISCOVERY_DOCS,
}).then(function() {
chrome.identity.getAuthToken({interactive: true}, async function(token) {
gapi.auth.setToken({
'access_token': token,
});
for(var i = 0; i < fileArray.length; i++) {
if (fileArray[i].type != "folder") {
const fileNameFile = fileArray[i].name;
const typeFile = fileArray[i].type;
const descriptionFile = fileArray[i].description;
let newType = ''
if(typeFile === 'docx') {
newType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
}
if(typeFile === 'pptx') {
newType = 'application/vnd.google-apps.presentation'
}
if(typeFile === 'xlsx') {
newType = 'application/vnd.google-apps.spreadsheet'
}
if(typeFile === 'pdf') {
newType = 'application/pdf'
}
var destFile = './src/Pages/downloads/' + fileNameFile + '.' + typeFile;
var newId = ''
var fileMetadata = {
'name': fileNameFile,
'description': descriptionFile,
'parents': [parentFolder]
};
console.log(fileMetadata)
var media;
await fetch('https://connect.smartpathed.com/makefile', {
method: 'POST',
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
body: JSON.stringify({
newType: newType,
fileDest: destFile,
})
})
.then(() => {
alert('next step activated')
fetch('https://connect.smartpathed.com/getfile')
.then(res => res.json)
.then(data => media = data)
.then(data => console.log("this is the body of topfile: " + data))
})
gapi.client.drive.files.create({
resource: fileMetadata,
media: media,
fields: 'id'
})
.then(function (err, file) {
if (err) {
console.log("Error for file creation: " + JSON.stringify(err));
console.log(media);
} else {
console.log("file download complete." + file);
newId = file.id;
}
})
}
Run Code Online (Sandbox Code Playgroud)
发布请求(Node.js 后端):
app.post('/makefile', (req, res) => {
const newType = req.body.newType;
console.log("this is the file type in post request: " + newType);
const dest = req.body.fileDest;
console.log("this is the file destination: " + dest);
var media = {
mimeType: newType,
body: fs.createReadStream(dest),
};
app.set("media", media);
})
app.get("/getfile", (req, res) => {
const file = req.app.get('media');
console.log(file);
res.send(file);
})
Run Code Online (Sandbox Code Playgroud)
所有这些代码都工作正常,直到涉及媒体请求以及通过drive.files.create 进行后续文件创建。我知道 fs.createReadStream 在后端工作,并且信息传输过来,因为我可以在日志中看到它。但是,我不知道是否可以将 fs.createReadStream 主体传输到客户端,并能够在 google.drive.create 函数的媒体请求中使用它。由于路径问题,这可能是不可能的。
话虽如此,有没有什么方法可以实现我在这里想要实现的目标,即将媒体变量传输到客户端,以允许通过gapi上传保存在后端的文件正文?
很高兴澄清这是否需要更多背景信息。
| 归档时间: |
|
| 查看次数: |
347 次 |
| 最近记录: |