hi everyone
import wixData from "wix-data";
export function buttonSave_click(event) {
let toInsert = {
"title": $w('#input1').value,
"description": $w("#textBox1").value,
"cover": $w("#uploadButton1").startUpload()
};
wixData.insert("VideoDatabaseOn", toInsert)
.then( (results) => {
let item = results; //see item below
} )
.catch( (err) => {
let errorMsg = err;
} );
}
Hey there,
To upload an image with the upload button to your database you’ll need to retrieve and store the url of the image into a global variable and then insert the url into your database.
For example:
let url; // global variable
$w("#myUploadButton").startUpload()
.then((uploadedFile) => {
let url = uploadedFile.url; // "wix:image://v1/68d3a9_1de7529c444b4c9eb38401f8efe0cad2.jpg/flowers.jpg#originWidth=1970&originHeight=1120"
})
.catch((uploadError) => {
let errCode = uploadError.errorCode; // 7751
let errDesc = uploadError.errorDescription; // "Error description"
});
export function buttonSave_click(event) {
let toInsert = {
"title": $w('#input1').value,
"description": $w("#textBox1").value,
"cover": url // url value of the image from the global variable
};
Feel free to check out the following article for reference:
Hope this was helpful! Good luck!
thank Miguel
my code work good by you idea
// upload start
export function buttonSave_click(event) {
$w("#uploadButton1").startUpload()
.then((uploadedFile) => {
let url = uploadedFile.url; // "wix:image://v1/68d3a9_1de7529c444b4c9eb38401f8efe0cad2.jpg/flowers.jpg#originWidth=1970&originHeight=1120"
let toInsert = {
"title": $w('#input1').value,
"description": $w("#textBox1").value,
"num": $w("#dataset1").getTotalCount() +1,
"tags": $w('#input8').value,
"playList": $w("#iContinent").value,
"cover": url
};
wixData.insert("nameDatabaseOn", toInsert)
.then( (results) => {
let item = results; //see item below
} )
.catch( (err) => {
let errorMsg = err;
} );
})
}
// upload end
Hi,
I tried below code. “image1” in the console shows URL value but looks like its not getting assigned to global variable image1. If i display the variable in console it is showing as undefined “image2”
let image1 ;
await $w ( “#image” ). uploadFiles ()
. then (( uploadedFiles ) => { let image1 = uploadedFiles [ 0 ]. fileUrl
console . log ( ‘image1’ + image1 ) })
console . log ( 'image2 ' + image1 )
when used glob variable image1 to insert nothing is getting uploaded in database.
let toInsert = {
“firstName” : $w ( ‘#firstName’ ). value ,
“lastName” : $w ( ‘#lastName’ ). value ,
“image” : image1
};
console messages:
image1 wix:image://v1/45afa9_1a0b8cda6c434f8fb674f4d02f9cc718~mv2.jpg/eps-jpeg-files-barber-shop-poster-illustration-99059750.jpg#originWidth=1300&originHeight=1390
image2 undefined
appreciate your help