0

I'm looking for a method to Upload a file on my server using Javascript only. I've already tried some method with formData that I found on the forum, but nothing works.

var fileChoose = document.getElementById('file-select');

form.onsubmit =  async function(event) {
event.preventDefault();


// Récupère le fichier sélectionné
var files = fileChoose.files;

// Création d'un objet FormData
var formData = new FormData();
var req = new XMLHttpRequest();
filesConfig = files[0]; // Ajoute le fichier dans une variable

formData.append('fileLoad', filesConfig);

try {
  let r = await fetch('./Files', {method: "POST", body: formData});
  console.log("HTTP response code:",r.status);
  console.log(r);
} catch(e) {
  console.log("Il y'a une erreur...: ", e);
}

This method tells me that the file is successfully sent, but I can't find the file on my server.

Any idea?

Cheers.

2 Answers 2

2

JavaScript is Client-Side. You will not be able to upload files to a Server with it. You would need a Server-Side language, such as PHP.

Article on Client vs Server Side

Sign up to request clarification or add additional context in comments.

1 Comment

.......or Javascript...... (Node.js) This answer is wrong IMO
1

You have to use Ajax.

How to make Ajax Request

Ajax can call a function from the Server-Side language without loading the page.

So you write the function in your Server-Side language than pass the date via Ajax to the function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.