0

Good evening, I am a beginner, I am on an application where the client has to download one of the xml files, but when the file is in local folder it downloads it and when it is elsewhere, it says: GET https://localhost/dossier/data.xml 404 (Not Found).

How to make it accept the file no matter where it is?

This is the code:

<form id="parcourir">
   <input type="file" id="real-file" hidden="hidden" />
   <button type="button" id="custom-button">Parcourir</button> <br>
   <span id="custom-text">Aucun fichier selectionné</span>
</form>
<script>
   const realFileBtn = document.getElementById("real-file");
   const customBtn = document.getElementById("custom-button");
   const customTxt = document.getElementById("custom-text");
   
   customBtn.addEventListener("click", function () {
       realFileBtn.click();
   })
   realFileBtn.addEventListener("change", function () {
       if (realFileBtn.value) {
           customTxt.innerHTML = realFileBtn.value.match(/[\/\\]([\w\d\s\.\-\(\)]+)$/)[1];
           console.log(location.href);
           console.log(customTxt.innerHTML);
           myFunction(this);
   
       } else {
           customTxt.innerHTML = "no file choosen yet";
       }
       var xhttp = new XMLHttpRequest();
       xhttp.onreadystatechange = function () {
           if (this.readyState == 4 && this.status == 200) {
   
               myFunction(this);
           }
       };
       xhttp.open("GET", customTxt.innerHTML, true);
       xhttp.onload = (response) => {
           console.log(xhttp.response);
       }
       xhttp.send();
   })
   function myFunction(xml) {
       var xmlDoc = xml.responseXML;
       console.log(xmlDoc);
   }
</script>
coreuter
  • 3,331
  • 4
  • 28
  • 74
student
  • 3
  • 4
  • Does this answer your question? [Fetch request to local file not working](https://stackoverflow.com/questions/50007055/fetch-request-to-local-file-not-working) – Kundan Jul 30 '21 at 02:58
  • @Kundan can not this cannot be done with xmlhttprequest??? – student Jul 30 '21 at 03:08
  • 2
    You're saying "download". I think you mean "upload", as in "send the file to the server", right? That's what an `` tag is for. – Tim Roberts Jul 30 '21 at 03:25
  • @Tim Roberts, yes – student Jul 30 '21 at 10:13
  • You can't do a file upload with a GET request. It has to be a POST request with content type `multipart/form-data`. Where do you think the file contents are being sent in your example? – Tim Roberts Jul 30 '21 at 19:55
  • @Tim Robert , I do not know, precisely I ask myself the question but I know that it is not towards the servers – student Aug 02 '21 at 10:18
  • I'm just not sure what you're trying to do. If you changed the first line to `
    ` and the third line to ``, then your file would get shipped to the server with no Javascript needed.
    – Tim Roberts Aug 02 '21 at 16:26

0 Answers0