Hi, Need help to import external images. Process:
-
Import Image to Site Files under Media.
-
Add to Database for further consumption
For step1: Import Image to Site Files under Media created a bk.jsw file at the backend
//bk.jsw
import { mediaManager } from 'wix-media-backend';
export function importFile(url) {
return mediaManager.importFile(
"/ImgFiles/FileID1",
url,
{
"mediaOptions": {
"mimeType": "image/jpeg",
"mediaType": "image"
},
"metadataOptions": {
"isPrivate": false,
"isVisitorUpload": false,
"fileName": "my-image.jpg",
"context": {
"someKey1": "Later1",
"someKey2": "Later2"
}
}
}
);
}
Next on a page with a submit button (submitButton) added the following code.
This is not meant for visitors. later to be converted to backend scheduled code.
import {importFile} from 'backend/bk';
export async function submitButton_click(event) {
await importFile('alink.jpeg') //replace with a proper http:// url here instead of alink.jpeg. Forums will not allow me to use links
.then ((fle) => {
console.log ("Successfully Imported:" + fle);
let item1 = {
"emailId": "email",
"imgOne": fle.fileUrl
};
/* database mydb structure:
"emailId type text | imgOne type image | imgGallery type MediaGallery
*/
// UPLOAD TO DATABASE
wixData.insert("mydb", item1)
.then( (results) => {
let item = results; //see item below
} )
.catch( (err) => {
let errorMsg = err;
} );
console.log('item1 looked like: ', item1);
} );
}
Above code in step 1.2 gets me to a point where I get a file successfully uploaded to Media under the relevant folder “/ImgFiles/FileID1” and in the database it comes up with an new entry however the image field in database shows up like this.
similarly need to populate field imgGallery with type MediaGallery in database but clueless on how to achieve this. Believe I am missing something here. Please suggest.
Thanks in advance.