Issue getting google workspace file (JavaScript) - javascript

Im calling request from google workspace file with gapi using this code
gapi.client.drive.files.export({
fileId: fileId,
mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
})
from that code i get response back of base64 string to convert as a blob but when i open file in google docs or msword file is corrupt and not working but if i change mimeType into application/pdf it working
same thing also happen in if i request file with application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
am i doing something wrong here or i there is other way to getting file workspace
md5Checksum is not available

As mentioned in the official documentation for files.get:
Response
By default, this responds with a Files resource in the response body. If you provide the URL parameter alt=media, then the response includes the file contents in the response body. Downloading content with alt=media only works if the file is stored in Drive. To download Google Docs, Sheets, and Slides use files.export instead. For further information on downloading files, refer to Download files.

Finally i found solution for this issue according to this official documentation we have two type we can download from google drive.
here my code how i request for file
const res = await drive
.files
.export({
fileId: fileId,
mimeType: yourMimeTarget
})
.then(res => res)
const blob_file = fetch(`data:${yourMimeTarget};base64,${btoa(res.body)}`)
.then(res => res.blob())
so after you fetch google workspace file you will get base64 string than you can convert it into blob

Related

upload pdf file from storage to firebase in react-native

I have been trying to upload PDF file from phone storage to Firebase. Its working fine when I use react-native-document-picker to select a pdf file, which returns the path of the file something like content://com.android.externalstorage.documents/document/primary%3Adocs%2F' +'<filename>'.pdf'
which is then passed to fetch
const response = await fetch(path);
blob = await response.blob();
and then the blob is uploaded to Firebase.
But the problem is that I don't want to pick a document every time. if I don't use react-native-document-picker and just curate the exact path return by the module. it gives network error when passed to fetch, so unable to create a blob.
I also tried react-native-fs but couldn't figure out how to create a blob of PDF residing in emulated storage.

How to read contents off a zip file from google cloud storage (GCS) in Postman javascript

In postman response, the output is a URL that contains a https link to google cloud storage which contains a zip folder. This . zip folder contains an excel and a pdf file.
I am looking for one of these solutions.
Either use javascript to download the zip folder, parse it and validate the contents in the folder. i.e assert to ensure the content of the zip folder contains an excel and a pdf file.
Make a GET request directly to gcs API via encoded url to get the contents of the zip folder. At the moment I can get to gcs cloud API, it seems to provide me with metadata, not the contents of the zip folder
For the 2nd solution, I have tried using this URL to get to the GCS
https://www.googleapis.com/storage/v1/b/gcs-bucket-service/o/361b6d18-3881-49ed-994b-442574%2Freport_1.zip
This provides the metadata
I would be happy for either of the solutions to work
You can't have an AJAX request open the download prompt since you physically have to navigate to the file to prompt for download. Instead, you could use a success function to navigate to download.php. This will open the download prompt but won't change the current page.
$.ajax({
url: 'download.php',
type: 'POST',
success: function() {
window.location = 'download.php';
}
});

Sending .wav file to backend

I am currently using RecorderJS and need to send a .wav file to the backend. the API is quite limited in documentation so I am struggling to figure out how to send the .wav file through my axios.post(...).
I am able to download the .wav file with
Recorder.download(theblob, 'audio.wav');
this downloads a .wav file which I can play through itunes so it is the right format. I now need to figure out how to save this in a variable in order to post it through axios. Also, what should i use for me headers, .. ect?
Looking for any kind of javascript solution to this. I just need to send the exact downloaded file to my backend. Thanks!
The download method stores the file somewhere on your disk. I believe javascript cannot traverse your computer's path and read files for security reasons. I'm not sure if recorder-js offers storing it in a variable "out of the box", so you may want to get that handled first.
For your second part of the question:
This should work for posting it to the back-end:
let data = new FormData();
data.append('wavfile', file, file.name);
const config = {
headers: { 'content-type': 'multipart/form-data' }
}
axios.post('/api/recorderfiles', data, config)

how to copy Word Document contents to Google Docs using any command line tool or Scripts

I have a word doc in my local drive and I want to copy the whole content of the word doc to any specific google docs file using any Command line tool or script
Please let me know if you have any clarifications I will explain my requirements further
Thanks
Vimal
You can do this with Apps Script or Drive REST API.
In Apps Script advanced Drive service allows you to use the Google Drive web API in Apps Script. Much like Apps Script's built-in Drive service, this API allows scripts to create, find, and modify files and folders in Google Drive.
The following example demonstrates how to save a file to a user's Drive.
function uploadFile() {
var image = UrlFetchApp.fetch('DOCUMENT_EXPORT_URL').getBlob();
var file = {
title: 'google_logo.png',
mimeType: 'image/png'
};
file = Drive.Files.insert(file, image);
Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
}
Following this sample code, you will get the blob for the file and save it as the converted file.
This is a tutorial for converting documents(excel to spreadsheet)
I haven't tested this but for the Drive Rest API try using downloadURL to get the file in order to upload it as Google Format by setting the property convert=true (making a copy of it in google docs) or getting the blob saving it as Google docs.
Snippet for uploading file: setting the contentType.
var fileName = 'convertedFile';
var contentType = 'application/vnd.google-aps.document';
var metadata = {
'title': fileName,
'mimeType': contentType
};
Hope this helps!

Dropbox Core API 413 error on creating a share link

Im currently trying to create a share links for a pdf file that was just uploaded through my App while using the Dropbox Core API.
The code is below:
request.post('https://api.dropboxapi.com/1/shares/auto/proposals/'+name+'?short_url=false',{
headers: { Authorization: 'Bearer TOKEN HERE', 'Content-Type': 'application/pdf'
},body:content}, function optionalCallback (err, httpResponse, bodymsg) {
if (err) {
console.log(err);
}else{
console.log('Shared link ' + JSON.stringify(httpResponse));
}
});
Points to note:
The PDF file size is 11MB, I can successfully and easily upload the file to dropbox using the API.
The issue only arises when I try to create a share link for the recently uploaded 11MB file.
Also note I am using Node.JS to upload & create share links.
The Error:
The error I get is HTTP Error 413, which based on my research means "Request entity too large"
Below is an image of the error, its not the whole image as the error was too long:
The maximum file size for uploading through the API is 150MB and my file is way below the line. Is there a separate file size for generating share links?
Note
I have tested small files of size 1MB to 2MB and was successfully able to generate a share link, issue arises with large files i.e (11MB)
Based on the fact that you're sending a body and using a Content-Type of application/pdf, I'm going to guess that you're trying to upload a file with this API call, but that's not what /shares does. /shares is a way to create a shared link to a file that's already in Dropbox. You should upload with, e.g. /files_put, and then call /shares to create a shared link to that file.

Categories