# File document upload issue using wixData.insert

**URL:** <https://forum.wixstudio.com/t/file-document-upload-issue-using-wixdata-insert/21395>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [April 25, 2019, 2:42pm UTC](https://forum.wixstudio.com/t/file-document-upload-issue-using-wixdata-insert/21395 "2019-04-25T14:42:42Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![timstumbo](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/timstumbo/32/9576_2.png) [@timstumbo](https://forum.wixstudio.com/u/timstumbo)\
**Post date:** [April 25, 2019, 2:42pm UTC](https://forum.wixstudio.com/t/file-document-upload-issue-using-wixdata-insert/21395/1 "2019-04-25T14:42:42Z")

</div>

I’m using the below code to submit information into the database. Everything is working but the file itself. It appears to work but when I look in the database it shows an issue with the file (see attached picture). What am I missing?

Thanks for the help!

```javascript
import wixData from 'wix-data';
import wixLocation from 'wix-location';

export function uploadButton3_change(event) {
let file = $w("#uploadButton3").value[0].name;
$w("#text26").text= String(file);
}

export function button1_click_1() {
 let toInsert = { 
 "repeater_file1": $w("#uploadButton3").value, 
 "repeaterFile1Title": $w("#text26").text,
 "repeater_date": $w("#datePicker1").value,
 "repeater_title": $w("#inputTitle").value,
 "repeater_description": $w("#textDescription").value,
          };
 
wixData.insert("Announcements", toInsert)
           .then(() => {
           wixLocation.to("https://www.blank.com/announcements");
           })
           .catch( (err) => {
             console.log(err);
           } );
}

```

![](https://us1.discourse-cdn.com/wix/original/3X/c/2/c20ab6f9695cbe84e8de83555537aafb2f4e9c28.png)

---

<div class="post-metadata">

**Author:** ![timstumbo](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/timstumbo/32/9576_2.png) [@timstumbo](https://forum.wixstudio.com/u/timstumbo)\
**Post date:** [April 25, 2019, 3:52pm UTC](https://forum.wixstudio.com/t/file-document-upload-issue-using-wixdata-insert/21395/2 "2019-04-25T15:52:11Z")

</div>

I think I understand what I’m missing I’m just not sure how to incorporate it into my existing code. I missing that actually file upload function, something like this:

```javascript
export function button1_click(event) {
 if($w("#uploadButton1").value.length > 0) {
    $w("#uploadButton1").startUpload()
      .then( (uploadedFile) => {
        $w("#").src = uploadedFile.url;
      })

```

I think need to figure out how to get the “uploadFile.url” into the “repeater\_file1” field in the database.

---

<div class="post-metadata">

**Author:** ![Shan](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/shan/32/106059_2.png) [@Shan](https://forum.wixstudio.com/u/Shan)\
**Post date:** [April 25, 2019, 8:11pm UTC](https://forum.wixstudio.com/t/file-document-upload-issue-using-wixdata-insert/21395/3 "2019-04-25T20:11:21Z")

</div>

Hi Tim,

Indeed you have not uploaded the file but you are just passing the value of the upload button. Try this:

```javascript
import wixData from 'wix-data';
import wixLocation from 'wix-location';

export function uploadButton3_change(event) {
    let file = $w("#uploadButton3").value[0].name;
    $w("#text26").text= String(file);
}

export function button1_click_1() {
    if($w("#uploadButton3").value.length > 0) { //run only if a document is uploaded
          $w("#button1").disable();
          $w("#button1").label = 'Please wait...';
          $w("#uploadButton3").startUpload()
          .then( (uploadedFile) => {
                let fileLocation = uploadedFile.url;
                insertData(fileLocation);
          });
      } else {
          $w("#button1").label = 'Please upload file';
      }
}

function insertData(fileLocation) {
    let toInsert = { 
         "repeater_file1": fileLocation, //make sure that repeater_file1 is a document field in the database
         "repeaterFile1Title": $w("#text26").text,
         "repeater_date": $w("#datePicker1").value,
         "repeater_title": $w("#inputTitle").value,
         "repeater_description": $w("#textDescription").value,
    };
    wixData.insert("Announcements", toInsert) //name of the database
    .then(() => {
           wixLocation.to("https://www.blank.com/announcements"); //this is where your user will be redirected after the file upload so enter a proper URL
    });
}

```

---

<div class="post-metadata">

**Author:** ![timstumbo](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/timstumbo/32/9576_2.png) [@timstumbo](https://forum.wixstudio.com/u/timstumbo)\
**Post date:** [April 25, 2019, 8:24pm UTC](https://forum.wixstudio.com/t/file-document-upload-issue-using-wixdata-insert/21395/4 "2019-04-25T20:24:44Z")

</div>

Shan, thanks for the response! Ok, so the code seems to be working for the most part. The file is now uploading correctly and is inserted into the database. I’m using this in a lightbox and the one issue I’m having now is it hangs on “Please wait…” and nothing ever happens. However if I manually close the lightbox and then refresh the page everything shows correctly.

---

<div class="post-metadata">

**Author:** ![Shan](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/shan/32/106059_2.png) [@Shan](https://forum.wixstudio.com/u/Shan)\
**Post date:** [April 25, 2019, 8:29pm UTC](https://forum.wixstudio.com/t/file-document-upload-issue-using-wixdata-insert/21395/5 "2019-04-25T20:29:57Z")

</div>

@timstumbo Please wait means that the file is still uploading, it does take few seconds.

Can you screenshot your code

---

<div class="post-metadata">

**Author:** ![timstumbo](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/timstumbo/32/9576_2.png) [@timstumbo](https://forum.wixstudio.com/u/timstumbo)\
**Post date:** [April 25, 2019, 8:34pm UTC](https://forum.wixstudio.com/t/file-document-upload-issue-using-wixdata-insert/21395/6 "2019-04-25T20:34:53Z")

</div>

@shantanukumar847 It hangs for several minutes. That’s when I finally just closed the lightbox did a page refresh and everything was there correctly. Here’s the exact code I’m using on the page (except for the wixLocation address, I modified that before copying the code over):

```javascript
import wixData from 'wix-data';
import wixLocation from 'wix-location';

export function uploadButton3_change(event) {
 let file = $w("#uploadButton3").value[0].name;
    $w("#fileTitle3").text= String(file);
}

export function submitButton_click() {
 if($w("#uploadButton3").value.length > 0) { 
          $w("#submitButton").disable();
          $w("#submitButton").label = 'Please wait...';
          $w("#uploadButton3").startUpload()
          .then( (uploadedFile) => {
 let fileLocation = uploadedFile.url;
                insertData(fileLocation);
          });
      } else {
          $w("#submitButton").label = 'Please upload file';
      }
}

function insertData(fileLocation) {
 let toInsert = { 
 "repeater_file1": fileLocation,
 "repeaterFile1Title": $w("#fileTitle3").text,
 "repeater_date": $w("#datePicker1").value,
 "repeater_title": $w("#inputTitle").value,
 "repeater_description": $w("#textDescription").value,
    };
    wixData.insert("Announcements", toInsert)
    .then(() => {
           wixLocation.to.to("https://www.website.com/announcements");
    });
}

```

---

<div class="post-metadata">

**Author:** ![Shan](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/shan/32/106059_2.png) [@Shan](https://forum.wixstudio.com/u/Shan)\
**Post date:** [April 25, 2019, 8:37pm UTC](https://forum.wixstudio.com/t/file-document-upload-issue-using-wixdata-insert/21395/7 "2019-04-25T20:37:02Z")

</div>

@timstumbo see the 3rd last line. There should only be one .to

wixLocation.to

---

<div class="post-metadata">

**Author:** ![timstumbo](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/timstumbo/32/9576_2.png) [@timstumbo](https://forum.wixstudio.com/u/timstumbo)\
**Post date:** [April 25, 2019, 8:39pm UTC](https://forum.wixstudio.com/t/file-document-upload-issue-using-wixdata-insert/21395/8 "2019-04-25T20:39:50Z")

</div>

@shantanukumar847 That fixed it, I’m not sure how I got that extra .to in there! I owe you big time, thank you!

---

<div class="post-metadata">

**Author:** ![timstumbo](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/timstumbo/32/9576_2.png) [@timstumbo](https://forum.wixstudio.com/u/timstumbo)\
**Post date:** [April 26, 2019, 4:01pm UTC](https://forum.wixstudio.com/t/file-document-upload-issue-using-wixdata-insert/21395/9 "2019-04-26T16:01:18Z")

</div>

@shantanukumar847 I have one one more question. Sometimes they won’t need to submit any files, they’ll just submit a title and description in the form (this is something for a contributor to post announcements). With the current code it doesn’t allow you to submit without uploading a file. I removed the following code but it does nothing after clicking on the submit button.

```javascript
      } else {
          $w("#submitButton").label = 'Please upload file';
      }

```

Also, I’m going to have 3 uploadButtons so they can submit more than one file, will I just be able to duplicate the code for the additional buttons?

Thanks!

---

<div class="post-metadata">

**Author:** ![admin46600](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/admin46600/32/84050_2.png) [@admin46600](https://forum.wixstudio.com/u/admin46600)\
**Post date:** [April 4, 2020, 5:46am UTC](https://forum.wixstudio.com/t/file-document-upload-issue-using-wixdata-insert/21395/10 "2020-04-04T05:46:56Z")

</div>

Hello! I just used this code to help me code two upload buttons on my site. Many thanks to you (@timstudumbo & @shan) for that!

---

<div class="post-metadata">

**Author:** ![admin46600](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/admin46600/32/84050_2.png) [@admin46600](https://forum.wixstudio.com/u/admin46600)\
**Post date:** [April 4, 2020, 5:48am UTC](https://forum.wixstudio.com/t/file-document-upload-issue-using-wixdata-insert/21395/11 "2020-04-04T05:48:56Z")

</div>

@timstumbo Sorry! I sent my last comment before I was done typing! My question is about making the upload buttons non-required fields. Did you ever figure out how to do that?

---

<div class="post-metadata">

**Author:** ![admin46600](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/admin46600/32/84050_2.png) [@admin46600](https://forum.wixstudio.com/u/admin46600)\
**Post date:** [April 4, 2020, 5:50am UTC](https://forum.wixstudio.com/t/file-document-upload-issue-using-wixdata-insert/21395/12 "2020-04-04T05:50:46Z")

</div>

@shantanukumar847 Hello, Shan! Would you be able to help fix the code below so that users who do not have an upload can successfully submit the form? Thanks!!!

```javascript
export function clientUpload1_change(event) {
let file = $w("#clientUpload1").value[0].name;
$w("#fileTitle1").text= String(file);
}
export function clientUpload2_change(event) {
let file = $w("#clientUpload2").value[0].name;
$w("#fileTitle2").text= String(file);//Add your code for this event here:
}
export function buttonSubmitGo_click() {
if($w("#clientUpload1").value.length > 0) {
$w("#buttonSubmitGo").disable();
$w("#buttonSubmitGo").label = 'Please wait...';
$w("#clientUpload1").startUpload()
.then( (uploadedFile) => {

let fileLocation = uploadedFile.url;
insertData(fileLocation);
});
} else {
$w("#buttonSubmitGo").enable();
$w("#buttonSubmitGo").label = 'Submit';
}
}
function insertData(fileLocation) {
let toInsert = {
"clientUpload1": fileLocation,
"clientUpload2": fileLocation,
"clientId": $w("#clientId").value, //clientId,
"email": $w("#email").value, //userEmail,
"fullName": $w("#fullName").value,
"projectType": $w("#projectType").value,
"projectCategory": $w("#category").value,
"planType": $w("#planType").value,
"projectTitle": $w("#projectTitle").value,
"projectDetails": $w("#projectDetails").value,
"projectStatus": $w("#projectStatus").value
};
wixData.insert("Projects", toInsert)
.then(() => {
wixLocation.to("/success");
});
}

```

* * *

---

<div class="post-metadata">

**Author:** ![millwards322](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/millwards322/32/13622_2.png) [@millwards322](https://forum.wixstudio.com/u/millwards322)\
**Post date:** [June 24, 2020, 6:40pm UTC](https://forum.wixstudio.com/t/file-document-upload-issue-using-wixdata-insert/21395/13 "2020-06-24T18:40:00Z")

</div>

hi how would this work if i wanted to upload more than 1 file document with using 2-4 upload buttons? thanks
