Can get a correction of this code to save a file through a save window. Thanks

Please I am trying to write a code to download a file through a save window the file is kept in a database sample collection

PAGE CODE

import wixlocation from ‘wix-location’;
import myGetDownloadUrl from ‘backend/media’;

export function button1_click(event) {

myGetDownloadUrl
.then((myFileDownloadUrl) => {
wixlocation.to(myFileDownloadUrl);
});
}

BACKEND CODE

import wixData from ‘wix-data’;
import { mediaManager } from ‘wix-media-backend’;

export async function myGetDownloadUrl() {
return wixData.query(“sample collections”)
.find()
.then(async(results) => {
if(results.items.length > 0) {

  let item = results.items[0];
 const myFileDownloadUrl = await mediaManager.getDownloadUrl(item.document, 1);  
 return myFileDownloadUrl;
} else {
  return{success:false};
 }

});

Hi,

Although I do not have knowledge of your collections structure, it seems you are retrieving the wix formatted url from a collection.

Depending on the file type it should look something like this:

wix:video://v1/...

This seems correct.

However, I noticed that on your page code you imported myGetDownloadUrl() as a default export and did not invoke it.

I would try this out instead:

Original Code

import wixlocation from ‘wix-location’;
import myGetDownloadUrl from ‘backend/media’;

Change

import wixLocationFrontend from 'wix-location-frontend';
import { myGetDownloadUrl } from ‘backend/media’;

Original Code

myGetDownloadUrl
    .then((myFileDownloadUrl) => {
        wixlocation.to(myFileDownloadUrl);
    });

Change

myGetDownloadUrl()
    .then((myFileDownloadUrl) => {
        wixLocationFrontend.to(myFileDownloadUrl);
    });