我正嘗試使用Javascript將以下文件上傳到我的Google雲端硬盤,並使用Google Developer's示例。該文件由用戶選擇過程即時創建,然後自動下載到默認目錄(下載)。有沒有辦法將文件同時放置在Google雲端硬盤上?我使用jsPDF,並嘗試了一些像下面這樣的東西,但不成功。將PDF文件上傳到Google雲端硬盤
E:\Downloads\MyFile.pdf
我繞過文件選取器,因爲我知道要上傳哪個文件。我修改了Google示例以將代碼傳遞給文件名,但它失敗了。
我想我需要在我的硬盤上創建一個Blob文件,然後傳遞給Google API,但我嘗試了一些沒有成功的組合。任何幫助表示讚賞。
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<script type="text/javascript">
var CLIENT_ID = 'my-client-id';
var SCOPES = 'https://www.googleapis.com/auth/drive';
var FOLDER_ID = 'my-drive-folder-id';
/**
* Called when the client library is loaded to start the auth flow.
*/
function handleClientLoad() {
window.setTimeout(checkAuth, 1);
}
/**
* Check if the current user has authorized the application.
*/
function checkAuth() {
gapi.auth.authorize(
{'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': true},
handleAuthResult);
}
/**
* Called when authorization server replies.
*
* @param {Object} authResult Authorization result.
*/
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
var MyFile = "E:\PAPA\Downloads\test.pdf";
insertFile(MyFile);
}
}
/**
* Start the file upload.
*
* @param {Object} evt Arguments from the file selector.
*/
function uploadFile(evt) {
gapi.client.load('drive', 'v2', function() {
var file = evt.target.files[0];
insertFile(file);
});
}
/**
* Insert new file.
*
* @param {File} fileData File object to read data from.
* @param {Function} callback Function to call when the request is complete.
*/
function insertFile(fileData, callback) {
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var reader = new FileReader();
reader.readAsBinaryString(fileData);
reader.onload = function(e) {
var contentType = fileData.type || 'application/octet-stream';
var metadata = {
'title': fileData.name,
'mimeType': contentType,
'parents': [{"id":FOLDER_ID}]
};
var base64Data = btoa(reader.result);
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
base64Data +
close_delim;
var request = gapi.client.request({
'path': '/upload/drive/v2/files/',
'method': 'POST',
'params': {'uploadType': 'multipart'},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody});
if (!callback) {
callback = function(file) {
console.log(file)
};
}
request.execute(callback);
}
}
</script>
<script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
</head>
<body>
<!--Add a file picker for the user to start the upload process -->
<input type="file" id="filePicker" style="display: none" />
<input type="button" id="authorizeButton" style="display: none" value="Authorize" />
</body>
</html>
嗨@Quentin,我明白了。有沒有辦法做到這一點?用戶根據所做的選擇創建PDF,然後自動下載到Downloads文件夾。有沒有辦法在Google Drive上同時放置副本? – TheRealPapa
如果你正在創建它的客戶端,那麼可能,但你必須使用創建PDF的數據來創建文件對象,而不是將它保存到的地方。如果你在服務器端創建它,那麼你必須從服務器端代碼將它發送到Dropbox。 – Quentin
感謝您的回覆。我用jsPDF創建客戶端,然後將其保存在本地硬盤上。所以在保存文件之前創建文件對象?如何搜索這個例子? Thansk! – TheRealPapa