Back

getFlieView give full text file instead of URL.href node-sdk

  • 0
  • Functions
Luky
27 Aug, 2023, 22:25

Hi !

Calling getFlieView form the node sdk give a buffer containing the full text of the file instead of URL.href ?

TypeScript
const csvFile = await sdk.InputFile.fromPlainText(csvString, `batch_${batchId}.csv`);
const storageReponse = await storage.createFile("batch_cards_csv", batchId, csvFile);
const fileView = await storage.getFileView("batch_cards_csv", storageReponse.$id);
// Debugging
console.log("type", typeof fileView);
console.log("fileview Object", fileView);
console.log("fileview", fileView.toString());
console.log("href", fileView.href);

log output:

TypeScript
type object
fileview Object {"type":"Buffer","data":[49,44,104,116,116,112,115,58,47,47,108,11 ...
fileview 1,https://localhost:5173/card/nwbEC
2,https://localhost:5173/card/RSugw
3,https://localhost:5173/card/60Sm8
4,https://localhost:5173/card/vPf73...

Is there another way to get the href or i am missing something ?

PS: The file is correctly created and i can get the url in the appwrite UI. But not in the sdk.

TL;DR
The user is using the `getFlieView` method in the Node SDK and noticing that it returns a buffer with the full text of the file instead of the URL.href. The user is currently using a workaround by creating a hidden link and clicking on it to download the file. The user wants to know if there is a better way to get the URL for the file. Solution: Instead of using the `getFlieView` method, the user can use the `getFileDownload` method in the Web SDK to get the URL for the file. They can then use this URL to automatically download the file on the client side
Drake
27 Aug, 2023, 23:21

Yes, this is expected. The url to the file is the path in the docs: https://appwrite.io/docs/server/storage?sdk=nodejs-default#storageGetFileView

Drake
27 Aug, 2023, 23:22

Why do you need the url?

Luky
27 Aug, 2023, 23:35

Yeah this work too. I need the url to make the client download the file automatically. If there is another way than using an hidden link and click() it would be better than this solution. I got it working by getting the link using the web-sdk :

TypeScript
const storageResponse = await storage.getFileDownload("batch_cards_csv", batchId);
const link = await createClientLink(storageResponse.href);
Luky
27 Aug, 2023, 23:49

I download the file via xhr and then use

TypeScript
const link = (window.URL ? window.URL : window.webkitURL).createObjectURL(blob);
setDownloadLink(link);

set the link to an hidden link

TypeScript
<a ref={downloadLinkRef} href={downloadLink} download={`batch.csv`} style={{ display: "none" }}></a>

then click on it

TypeScript
downloadLinkRef.current.click();

So the user doesn't leave the current page.

Drake
27 Aug, 2023, 23:54

Yes, this is the correct way to do it client side

Luky
27 Aug, 2023, 23:57

Ok thanks for your reactivity !

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more