How to upload file to collection (using code)?

I’m currently trying to build a solution that can upload a file with some additional information into a collection by using code. So far, I’ve managed to insert a file into the collection and then the additional information on separate rows, but obviously I would like to have everything on the same row.

I have two buttons - one upload button for choosing the file to upload, and a submit button to save everything to the collection. I have used “Connected to data” to save the file to the collection.

The code written thus far is as follows:

import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;

export function submitButton_click(event) {
let user = wixUsers.currentUser;

let userID = user.id;
let isLoggedIn = user.loggedIn;

let newForm = {
title: “”,
doc: “xx The actual document uploaded”,
docType: “TechForm”,
validated: false ,
userId: userID
}
wixData.save(“TechForm”, newForm);
}

How can I add the document to the “doc” variable above? Or does anyone have a better solution on how to solve this?

Hello,

If you want to work only with code, you will need to trigger the upload process through code as well instead of using the “connect to data” options.
See the first example in the upload button’s reference: https://www.wix.com/code/reference/$w.UploadButton.html
Notice how it logs “uploadedFile.url” to the console. You would instead save it as the “doc” attribute of your form.

Good luck

Thank you, this seems to be working flawlessly!