How do I upload a FilePond file field to the server along with other form elements. For example i have a form element that contains input fields, a select field, a textarea and a file input field which is connected to the Filepond plugin.
JavaScript
x
7
1
<form class="uploadform">
2
<input type="text" placeholder="type the title of the document you are uploading">
3
<textarea class="textarea" placeholder="type anything here"></textarea>
4
<input type="file" class="filepond" name="file[]" multiple>
5
<button type="button" class="uploadbtn">Upload Document</button>
6
</form>
7
But whenever i try to upload the form the file field doesn’t upload with it to the server, please help me with this. How do i bind the FilePond input field to my formdata element?
This is my jQuery upload code:
JavaScript
1
12
12
1
$(".uploadbtn").click(function(){
2
var form = document.getElementsByClassName("uploadform")[0]
3
var formdata = new FormData(form)
4
$.ajax({
5
url: "path/to/php-server-side/request/handler.php",
6
data: formdata,
7
processData: false,
8
contentType: false,
9
method:"post"
10
}).done(function (response) {
11
})
12
Advertisement
Answer
You need to append FilePond files manually to the FormData object. FilePond objects has a file
property which is the original file blob object. You can go like this:
JavaScript
1
31
31
1
$(document).ready(function(e){
2
pond = FilePond.create(
3
document.querySelector('#file'), {
4
allowMultiple: true,
5
instantUpload: false,
6
allowProcess: false
7
});
8
9
$("#uploadform").submit(function (e) {
10
e.preventDefault();
11
var formdata = new FormData(this);
12
// append FilePond files into the form data
13
pondFiles = pond.getFiles();
14
for (var i = 0; i < pondFiles.length; i++) {
15
// append the blob file
16
formdata.append('file[]', pondFiles[i].file);
17
}
18
19
$.ajax({
20
url: "path/to/php-server-side/request/handler.php",
21
data: formdata,
22
dataType: 'JSON',
23
processData: false,
24
contentType: false,
25
method:"post"
26
}).done(function (response) {
27
// todo
28
});
29
30
})
31
});
JavaScript
1
13
13
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
2
<script src="https://unpkg.com/filepond/dist/filepond.min.js"></script>
3
<script src="https://unpkg.com/jquery-filepond/filepond.jquery.js"></script>
4
<link href="https://unpkg.com/filepond/dist/filepond.css" rel="stylesheet"/>
5
<script src="https://unpkg.com/filepond/dist/filepond.js"></script>
6
7
<form id="uploadform" enctype="multipart/form-data">
8
<input type="text" placeholder="type the title of the document you are uploading">
9
<br>
10
<textarea class="textarea" placeholder="type anything here"></textarea>
11
<input id="file" type="file" class="filepond" name="file">
12
<button type="submit" class="uploadbtn">Upload Document</button>
13
</form>
With this, you’ll get the files as if they where sent by html
form.