-
-
Notifications
You must be signed in to change notification settings - Fork 807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uploading File manually #24
Comments
@Quadriphobs1 It's not possible to upload with direct post ( as you cannot assign files to a file input, this is where the File Encode plugin is useful ). You can call the processFiles method to start uploading files manually. Use the getFile method to retreive specific file items. You can then send those to the server manually. edit in 2023: This means that if you have a form with a file input and server side logic that handles the file(s) upload, you can add FilePond to you file input and you won't have to make changes on the server. No need for the file encode plugin. |
Okay, I seems to get that part but where am little confused is the |
@Quadriphobs1 Ah, tested, and indeed it didn't work when omitting arguments. Just released a fix version |
Although I don't seem to get what you meant but what I am trying now is I created |
If I understand correctly you want:
Is that correct? If so, can you describe which step is difficult and why? |
I need to submit a form and other fields along with the files in an AJAX post. Would it be possible to attach and add the base64 encoded data to my request? What I'm aiming for is to submit both the file (base64 encoded) and other fields I need. {
username: '*****',
password: '*****',
imgdata: '<base64 encoded file as a string>' // or Object that filePond is attempting to send
} If I'm making the right assumptions I would have to read in each file inside the instance manually. I would still like to see the upload progress being updated on the filePond instance when I manually submit the form. Would this be possible? |
@DamSenViet You can use the File Encode plugin to have FilePond encode the files as base64 data. You can then send it along with a normal form post ( not using AJAX ). If you want to send data asynchronously you can use a FormData object to store all the values and files, this is what FilePond uses to send data to the server. To add data to files you can use the setMetadata method on the file object, this data will be sent along with the file. If you need to customize the request further you can set up a custom processing method, it provides hooks to control the FilePond interface ( progress indicators and errors/success state ). |
So finally I got to upload a file and in my controller I return the generated ID for the file but how would I add the return key to an hidden input and since I would prefer using the file-encode-plugin, how can someone attach the file-encode to an input filed... am just lost a little bit on it |
If you're using the File Encode plugin you don't need a |
@rikschennink Could you provide an example of how to add the encoded files to a FormData object? I'm having trouble figuring it out. |
@nifte check out the add files before encode. The op dropped a codepen that checks on the element with the img data. |
@DamSenViet I'm not seeing that codepen anywhere... 😅 Also, I believe I'm trying to do exactly what you were trying to do. Did you figure out a way to add the files along with other form inputs to your FormData for an AJAX-based submit? |
Oh my bad, it was an issue and I was typing on my phone. Here ya go: The issue's been fixed, but the codepen in the thread should show you how to extract the data. |
@DamSenViet So, I figured out how to access the Base64-encoded data for each file, but I'm still confused on how to actually attach them to a FormData object like @rikschennink mentioned above... |
@nifte C'mon man we're all developers but you gotta learn how to use the APIs. https://developer.mozilla.org/en-US/docs/Web/API/FormData/append Really not that hard to Google "formData". |
@DamSenViet I know how to use the FormData object. The part I'm confused on is what to actually append to the FormData (i.e. the 'value' parameter). What am I supposed to do with the JSON that Filepond gives me? |
@nifte well the file is base64 encoded. Get the base64 string via the data json and attach that to your FormData. Send that off then process the data server side (save/write in base64 mode, etc). |
@DamSenViet Okay, this is what I've got: var form = new FormData();
for (let i = 0; i < $('.filepond--list input').length; i++) {
let file = JSON.parse($('.filepond--list input').eq(i).val());
form.append('file' + i, file.data);
} I'm taking the data from each hidden input, and appending it to the FormData object. But when I check the |
That's exactly what a file is. It's literally just a giant string, usually in binary as 0's & 1's. If you open a file in binary reading mode that's literally all you will see. When you use base64 encoding it helps compress the binary representation. e.g. Here's why we base64 You can either send a string that's either 4 million bytes you can send a string that's 1 million bytes. It's pretty obvious what you should be picking. As the file size gets bigger, the benefits get better (relatively). If you're wondering why we can't send it in binary directly, there's really nothing stopping you if you know how to manipulate AJAX dataType. It's just the browser does not let you just grab the file data on your own. It's prohibited as a security measure. That's why we use the FileReader object to get the base64 representation, because it's an open API (and currently the only method available to us). That's also why we send the string off in base64 encoding. You take the string and write to a new file in base64 mode (turns base64 back into binary). If you can't write in base64 mode, decode the base64 string and write the resulting decoded string. In whatever language you're using you need to decode base64 the file. Base64 is standard so I doubt you'll have trouble finding a library. e.g. in a node.js server you would do something like this if you wanted to save the file somewhere:
That would be the entire upload operation
If this sounds foreign to you, I can't help you. |
@nifte If you're going to send FormData objects, why not use a custom |
@DamSenViet Thank you for the breakdown. I've never messed around with Base64, so I'll do some more research. I do understand the concept, though, from your explanation. I'm using PHP/Apache, as opposed to NodeJS, but I'm sure it works similarly. @rikschennink I already have a php file that handles files submitted from my form. I'm basically just trying to use Filepond as the UI for the file inputs in my form, so I'll use whatever the easiest solution is. |
@nifte If you’re submitting async set the |
@rikschennink Hello. import { FilePond, File, registerPlugin } from 'react-filepond';
handleSubmit(event){
event.preventDefault();
const pond = ????
pond.processFiles().then(files => {
// files have been processed
});
const data = new FormData();
data.append('name', this.state.name)
data.append('files', this.state.files)
...
data.append( ... )
axios
.post('/api/send.php', data, ...)
.then(...)
...
}
<form>
<TextField.../>
...
<TextField.../>
<Filepond ref={ref => this.pond = ref}
allowMultiple={true}
server="api/"
oninit={() => this.handleInit() }
onupdatefiles={(fileItems) => {
this.setState({
files: fileItems.map(fileItem => fileItem.file)
});
}}>
{this.state.files.map(file => (
<File key={file} src={file} origin="local" />
))}
</Filepond>
<button type="submit" >Submit</button>
</form> |
@GeSeRnnov <Filepond ref={ref => this.pond = ref}>
I advise to let FilePond send the files to the server, then when you submit the form with Axios you only send the file IDs returned by the server. Now the server knows which files have been uploaded and can move the files to the right location/database. If that's not what you want, then use |
@rikschennink, your advise was helpful for me. All works now)). |
How exactly do to we get the list of IDs back from filepond? I have the load(id); saving them as part of the process: function, but I haven't been able to find any documentation on getting that list back out? Technically I can make my own array with the unique IDs, but using filepond seems cleaner... I'm uploading multiple images to the server and saving them, then when they finish adding text on their post they hit 'Save'. At that point I want to send the post information, as well as the list of IDs corresponding to that post so the server can then do some stuff. |
@rikschennink thanks you. async onUpload() {
const { arrFiles } = this.state
console.log('arrFiles', arrFiles)
var data = new FormData();
_.forEach(arrFiles, file => {
data.append('ebookFile', file.file);
})
// data.append('hi', 'hung')
try {
const response = await uploadFiles(data)
console.log('response', response)
} catch (e) {
console.log(e)
}
} for those who come later. |
This was helpful for me for future people with this specific problem: |
How to work using this kind of Data . I am getting this result as an Array . There could be 10 images.
Any Suggestions |
@siddmegadeth In theory you should be able to add the path and FilePond will try to load the file from that location. You can do this with the If that doesn't work you can load the file object with FileReader and then pass it to FilePond. |
If possible can you provide exmaple. I am having a hardtime going through filepond documentation. |
@siddmegadeth If you have a filePond instance with a server location. const pond = FilePond.create({ server:'./api' });
pond.addFile('./path/to/file'); |
How can i send Profile ID or any user identification . I am not able to send user unique ID so that i can query this user and create new file or else append this file if user is found. Right now i cannot send information which i can extract in req.body Plus how do i trigger upload for above options I am using Cordova-Native-Photo and this the format i receive
I am not able to add this to filePond using pond.addFile('./path/to/file'); |
@siddmegadeth please post questions to StackOverflow as described in the issue template. This is related to Cordova not FilePond. |
Is that easy to understand? FormData + getFiles
|
This is my solution, inspired by @zyyoo
|
thanks for this great project and sorry for reopening this issue, the FilePond component : <FilePond allowFileSizeValidation fileMetadataObject={{ onpreparefile: (file, output) => { },
/> in the upload function i access the files using:
also imageTransformBeforeCreateBlob doesn't add text i appreciate help fixing this. |
While this question might be a bit dated, here's a response for individuals currently encountering this issue. I don't know why I saw a lot of people saying that this is not possible. There is a workaround without limitations. If you have data, you just want to put it somewhere else. Solution :
PS : You might use CSS to hide the input type="file" because you cannot set it to hidden in this case. |
Is it possible to upload the file manually by letting the user click on a normal submit button alongside with a form either using ajax or direct post
The text was updated successfully, but these errors were encountered: