-
Notifications
You must be signed in to change notification settings - Fork 787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to upload PDF file to dropbox or google drive in React Native? #1
Comments
Hi @singhlovey , It's a mistake on the google docs end, and the correct link should be, Next the required headers for google drive upload are not correct, the ones in your request are only for dropbox, so you should try the above with this url, RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
// dropbox upload headers
Authorization : "Bearer access-token...",
'Dropbox-API-Arg': JSON.stringify({
path : '/img-from-react-native.png',
mode : 'add',
autorename : true,
mute : false
}),
'Content-Type' : 'application/octet-stream',
// Change BASE64 encoded data to a file path with prefix `RNFetchBlob-file://`.
// Or simply wrap the file path with RNFetchBlob.wrap().
}, RNFetchBlob.wrap(PATH_TO_THE_FILE))
.then((res) => {
console.log(res.text())
})
.catch((err) => {
// error handling ..
}) and where is your RNFetchBlob.wrap(PATH_TO_THE_FILE), the data to your file. Okay and back to the google drive thing, |
@harzkr Okay I have done the changes as mentioned above but i am not understanding which file path I need to pass under 'path' property of an object and for PATH_TO_THE_FILE. Here I am passing file path which exists under my project's folder. I am little bit confused here. can you please clarify which should be the correct file path? Thanks |
Okay so for the dropbox api, the path property of the object, inside And as you mention, yes, the PATH_TO_THE_FILE, is the path of the stored file inside your project. It may be in https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API for a complete idea of the file and directory system.(Please be careful while visiting the link, the project has been shifted, so make sure it's exactly the above, with wkh237, copy and paste it rather than clicking it) Well do remember that the path in I hope I was clear and you'd be able to resolve the issue! |
I have made the changes as per your suggestions.
But now I got error :- RNFetchBlob request error: TypeError: expected dynamic type NOTE:- I am using react-native-fs plugin to fetch path of storage files. Please let me know where I am wrong? |
I think the problem is with the //new line
var func = this
RNFS.readDir(RNFS.DocumentDirectoryPath)
.then((result) => {
console.log('GOT RESULT', JSON.stringify(result));
//new lines
func.setState({
size:result.size.toString(),
})
.then((contents) => {
// log the file contents
console.log(contents);
})
.catch((err) => {
console.log(err.message, err.code);
}); And call this function later, just be sure that the state has updated, RNFetchBlob.fetch('POST', 'https://www.googleapis.com/upload/drive/v3/filesuploadType=media',{
Authorization : String(user.accessToken),
'Content-Type' : 'application/octet-stream',
'Content-Length' : this.state.size
}, "RNFetchBlob-file://" + RNFS.DocumentDirectoryPath + '/demo.pdf')
.then((res) => {
console.log("response in success" + res.text())
})
.catch((err) => {
console.log("response in error" + err)
}) That should work |
Many thanks to you!!! QUERY :- How can I set pdf file name. currently it shows untitled. One more thing. I have done this for google drive. Now I want to do the same for dropbox. can you suggest any react native plugin for dropbox login or authentication so that I can get access token of dropbox? or how to authenticate user with dropbox in react native to upload file to dropbox as well? Thanks |
That's wonderful you got it working and you are welcome. Well coming to authenticating with dropbox api, since I haven't been using it in any case scenarios, I do not have an idea of the packages that might be available for react native for the same. However, you can check out auth0 it self, it's pretty reliable, darn easy to set up, is free for a limited time, https://auth0.com/authenticate/react-native/dropbox/ But the best I'll say is to directly use the dropbox api itself, which you must have seen here, https://www.dropbox.com/developers/documentation/http/documentation Right at the beginning itself under 'Authorization', they have provided everything you will need for authorization, make a
You will get a response object like this Then call this url with a
That should take care of the authentication, and as I mentioned I haven't used dropbox api, but the instructions on the docs are pretty simple and achieve the results in a straightforward manner. |
Thanks. I will try and get back to you if any problem. QUERY :- How can I set pdf file name. currently it shows untitled. Thanks |
Hey Hi @singhlovey Yeah the simple upload request for google drive is only for the file and not for any metadata, so no format will work which will upload the name to the file through that url. You can see more here https://developers.google.com/drive/v3/reference/files/create So there is resumable and multipart uploads, which you can check here, Both of them have direct sending of the metadata, but the body part of the request is a bit muddled up, you would have to create the multipart body exactly as specified there(and it does become a problem sometimes) Here are a few links discussing the metadata issue, you can have a look, They mainly achieve it using the drive file api from googleapis, you can check out the So basically there are a lot of options, but the simplest one( according to me), which I use, I post a request to //read the file and get the size, name etc. and set them in state variables
var func = this
const res = await fetch('https://www.googleapis.com/drive/v3/files', {
method: 'POST',
headers: {
'Authorization': auth,
'Content-Type':'application/json'
},
body:JSON.stringify({
"name": func.state.name,"mimeType": "audio/aac","description": "Stuff about the file"
}),
})
const json = await res.json()
console.log(json)
func.setState({
fileiden:json.id
}, function(){
RNFetchBlob.fetch('PATCH',`https://www.googleapis.com/upload/drive/v3/files/${func.state.fileiden}`,{
'Authorization' : auth,
'Content-Type' : 'audio/aac',
'Content-Length':func.state.size.toString()
},RNFetchBlob.wrap(func.state.target))
.then((res) => {
console.log("response",res.json())
})
.catch((err) => {
console.log("error",err)
})
}) You can get the job done, through any of the methods above, hope I was clear, if not, do ask me |
This issue seems to have been resolved and inactive. Closing due to inactivity. |
Update README.md with tip on project references
I use the same method to get the id from the file as above :
but i get 401. Any idea how to resolve this @harzkr |
Hey Hi @uendar |
Hash - changed to chunk size instead of reading entire file
Syncing with main branch, support for 0.60
[FEAT] Add Flow specs - New Arch Migration 1/n
Hi,
First of all thanks for creating this plugin. I have integrated this plugin and now the thing is I want to upload PDF file to Dropbox as well as Google Drive.
I am able to get access token. But not able to upload pdf file to google drive or Dropbox. I know I am missing some steps. Below is my code that I have implemented.
REQUEST :-
RNFetchBlob.fetch('POST', 'https://www.googleapis.com/upload/drive/v3?uploadType=media', {
Authorization : user.accessToken,
'data': JSON.stringify({
path : "http://xyz.com/" + this.state.pdf,
mode : 'add',
autorename : true,
mute : false
}),
'Content-Type' : 'application/pdf',
})
.then((res) => {
console.log("response in success " + JSON.stringify(res));
It will give below response inside success.
RESPONSE :-
response in success {"data":"Not Found","taskId":"njehngge5xm4hl33iynfog","type":"utf8","respInfo":{"respType":"","headers":{"alt-svc":"hq=":443"; ma=2592000; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="42,41,39,35"","server":"UploadServer","date":"Fri, 23 Mar 2018 06:50:13 GMT","content-length":"9","content-type":"text/html; charset=UTF-8","vary":"X-Origin","x-guploader-uploadid":"AEnB2UrlSTCFYVYabN_dzHqqr55dBKjosk8u0IQ3D4bcysJyp_KkaYSW4yrE2P4gziHgfKOUrZpfAxaSpBwV6D4-a0142hiYhA"},"redirects":["https://www.googleapis.com/upload/drive/v3?uploadType=media"],"timeout":false,"taskId":"njehngge5xm4hl33iynfog","state":"2","status":404,"rnfbEncode":"utf8"}}
Pleas help. I am stuck in this from past few days. Let me know if this is not sufficient information for you.
Thanks
The text was updated successfully, but these errors were encountered: