0
 <!--Upload-->
    <form method="post" action="upload" enctype="multipart/form-data">
        
        <!--Image input-->                                                     <!--For Preview-->
        <input class = "image_input" id = "ii" type="file" accept="image/*" onchange="loadFile(event)">

        <br /><br/>

        <!--Image holder-->                               <!--For centered-->
        <div id = "id" style="width: 300px; height: 300px; margin:0 auto;"; >
            <img class = "image_holder" id="ih"/>
        </div>

        <br></br>   

        <input type="button" value="UPLOAD" onclick="upload()" ></input>

        <script type="text/javascript" src="upload.js"></script> 
      
    </form>

This is my Html code. I will use uload() function in upload.js file, then how can I get input image data & send to upload() function & upload image to my /upload folder ?

upload.js

function upload() {

//Get image
let photo = document.getElementById("ii");

//Set data
let formData = new FormData();
formData.append("photo", photo);

try {
    //Upload
    fetch('/upload', {method: "POST", body: formData});
 } catch(e) {
    alert(e);
 }

//Alert
//alert("image uploaded");

}

it's failed..

Swallacova
  • 37
  • 4

0 Answers0