Comments (4)
Hi,
To access the cachedFileArray
in your example, simple view upload.cachedFileArray
after a user has chosen their files.
The cachedFileArray
is an array of File
objects that is always kept up to date with the latest actions the user has taken on the preview panel. When you're ready, this collection of File
objects can be uploaded to your backend. To do this, use a FormData
object on your frontend and append()
to the FormData
object the contents of your cachedFileArray
. Here is some example code using jQuery's
ajax
function:
var formElement = document.getElementById('entry-form');
var formData = new FormData(formElement);
formData.append('firstImage', upload.cachedFileArray[0]); // This will get the first image
$.ajax({
url: "entry.php",
type: "POST",
dataType: "json",
async: true,
data: formData,
contentType: false,
processData: false,
cache: false,
success: function(data) {
console.log(data)
},
error: function(error) {
console.log(error)
},
complete: function(data) {
console.log(data)
}
})
Of course, if you have multiple images, you'll want to loop over them and add them accordingly to your formData object.
Ok, so if you follow this pattern, and have your backend PHP set up correctly, you'll be able to access these files in the $_FILES
variable in the PHP - and more specifically, the $_FILES['firstImage']
variable according to this example.
Give that a shot, thank you.
from file-upload-with-preview.
Ok. Can you pleas make a simple example of uploading multiple images (loop) and with 2 text input.
var data = new FormData();
$.each($("input[type='file']")[0].files, function(i, file) {
data.append('file', upload.cachedFileArray[0]);
});
or the above is right.?
https://stackoverflow.com/q/59195429/3836908 please have a look at my code
from file-upload-with-preview.
Sure - here's a codepen with a complete example featuring a loop. I've commented the JavaScript code heavily to break down the steps. You then can continue on using the ajax
code I had given in my previous comment. You can use the Print
button below the panel to print to the console and actually build the FormData
instance. This will usually present itself as the Submit
button in your actual app.
Thank you.
from file-upload-with-preview.
Hopefully that codepen can help point you in the right direction. Thank you.
from file-upload-with-preview.
Related Issues (20)
- How to access already added images while editing a form? HOT 2
- FileUploadWithPreview is not a constructor (extended with examples) HOT 2
- May I use html_form submit to backend? HOT 2
- how to capture the event with jquery? HOT 1
- There is no name attribute to get the image on backed HOT 4
- Beginner overloaded HOT 2
- Can I use this inside react [just react] ? HOT 2
- Name attribute not present in sending request to server. HOT 6
- file_upload_with_preview__WEBPACK_IMPORTED_MODULE_4__.FileUploadWithPreview is not a constructor HOT 4
- Use in Next js and Tailwind HOT 3
- bug on chooseFile label HOT 2
- Question: can I attach event to element, not to window? HOT 12
- Save in local storage HOT 2
- How to attach to previously initialized instance of FileUploadWithPreview? HOT 4
- How to dynamically click the clear button? HOT 1
- Added event confusion with presets and new add HOT 3
- IE11 HOT 3
- How to Change Input file label? HOT 1
- Need to Show The Browse Button HOT 1
- Rendered order of PresetFiles
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from file-upload-with-preview.