Git Product home page Git Product logo

Comments (4)

johndatserakis avatar johndatserakis commented on May 24, 2024

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.

sanoj26692 avatar sanoj26692 commented on May 24, 2024

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.

johndatserakis avatar johndatserakis commented on May 24, 2024

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.

johndatserakis avatar johndatserakis commented on May 24, 2024

Hopefully that codepen can help point you in the right direction. Thank you.

from file-upload-with-preview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.