my problem is that when I upload a file with the uploadButton
it envokes the onChange()
which in its turn calls the uploadFiles()
and that returns an array of the files. From there I use a variable to store the result and then I get the 1st array element’s fileUrl
using this uploadResults[0].fileUrl
. So all is good and I get the URL, but upon upload to my Collection when I submit the form, the image does not show up. I have uploaded another image manually, and from what I can see the URL’s have the same format, but one is valid and the other is not. The only difference is that one is uploaded in the Site Files, and the other in Visitor Uploads.
URL of the code uploaded image
"wix:image://v1/2fb1f8_7d5c9aab31b94162bdb414729309ce3f~mv2.jpg/cheerful-indian-photographer-with-backpack-holding-2023-11-27-04-57-44-utc%20(1).jpg#originWidth=400&originHeight=400"
URL of manually uploaded image
"wix:image://v1/41615d_fd2d136ba5ea409e821a9eb0e2c661fe~mv2.jpg/photographer-speaking-to-client-in-studio-2023-11-27-05-05-38-utc(1).jpg#originWidth=400&originHeight=400"
How it appears in the Collection
As you can see Wix does not recognize this url, i have tried with other formats and other images, smaller sizes, bigger sizes, other buttons.
The code below is what I am using, it is a snippet of only the file upload as the other functionality is working as intended. Would really appreciate the help on this one.
import wixData from 'wix-data';
import { authentication, currentMember } from 'wix-members-frontend';
$w.onReady(async () => {
// Wait for current member to resolve
const user = await currentMember.getMember();
$w('#uploadButton1').onChange(async() => {
const uploadResults = await $w('#uploadButton1').uploadFiles();
console.log(uploadResults);
});
$w('#button14').onClick(async(uploadResults) => {
await handleFormSubmission(user);
await createLinkedContact(uploadResults);
});
});
async function createLinkedContact(uploadResults) {
const linkedContact = {
firstName: $w('#input49').value,
lastName: $w('#input48').value,
email: $w('#input47').value,
image: uploadResults[0].fileUrl,
phoneNumber: Number($w('#input46').value),
date: $w('#datePicker1').value.toISOString().split('T')[0],
};
try {
// Insert the linked contact into the Testing collection
const insertedLinkedContact = await wixData.insert('Testing', linkedContact);
console.log('Linked contact created:', insertedLinkedContact);
// Optionally, you can update the member profile or perform additional actions
} catch (error) {
console.error('Error inserting linked contact into Testing collection:', error);
}
}
John Doe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.