Sisyphus_TheHedgehog
Eternal Poster
Henlo po, may pamilyar ba dito paano mag upload ng image/file to specific folder using javascript balak ko sana maglagay sa page na pwede magupload ng image tapos sa gdrive mag sasave at pwede ma retrieve. Salamat
<script src="https://apis.google.com/js/api.js"></script>javascript
// Isama ang Google Drive API SDK
gapi.load('client', init);
function init() {
// I-set ang mga API credentials
gapi.client.init({
apiKey: 'YOUR_API_KEY',
clientId: 'YOUR_CLIENT_ID',
discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest'],
scope: 'https://www.googleapis.com/auth/drive.file'
});
// Sagutin ang mga kahilingan sa autentikasyon
gapi.client
.authorize({
'client_id': 'YOUR_CLIENT_ID',
'scope': 'https://www.googleapis.com/auth/drive.file',
'immediate': true
})
.then(function() {
// Kung matagumpay ang pag-authenticate, tawagin ang pag-andar ng pag-upload
uploadToDrive();
});
}
function uploadToDrive() {
// Pangalanan ang file na iu-upload
var fileName = 'sample-image.jpg';
// Makuha ang folder ID ng target folder
var folderId = 'FOLDER_ID';
// Gumawa ng isang bagong file
var fileContent = new Blob(['FILE_CONTENT'], { type: 'image/jpeg' });
var fileMetadata = {
name: fileName,
parents: [folderId]
};
var file = new File([fileContent], fileName, { type: 'image/jpeg' });
// Simulan ang pag-upload
var formData = new FormData();
formData.append('metadata', new Blob([JSON.stringify(fileMetadata)], { type: 'application/json' }));
formData.append('file', file);
// Tawagin ang Google Drive API para sa pag-upload ng file
gapi.client
.request({
'path': '/upload/drive/v3/files',
'method': 'POST',
'params': { 'uploadType': 'multipart' },
'headers': { 'Content-Type': 'multipart/form-data' },
'body': formData
})
.then(function(response) {
console.log('File uploaded successfully: ', response.result);
})
.catch(function(error) {
console.error('Error uploading file: ', error);
});
}
'YOUR_API_KEY' at 'YOUR_CLIENT_ID' ng mga API credentials na iyong nakuha. Pati na rin ang 'FOLDER_ID' ng ID ng folder sa iyong Google Drive kung saan mo nais i-save ang imahe.