Click here to Skip to main content
15,883,819 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all !,

So i am trying to upload an image from frontend(html) and save it locally , get the path and save the path into the db. Here is my code:

Html:
  <!-- ... form-->
   <div class="form-group">
      <input type="file" id="prop_image" name="prop_image" accept="image/*">
  </div>
<div class="form-group">
                  <input type="submit" class="btn btn-primary" id="submit" ng-click="createProperty()"; name="submit" value="Create Property">
                </div><


javascript :
$("#PropertyForm").submit(function(event) {
         event.preventDefault();
   roomForm = new FormData();
         roomForm.append("room_no",$scope.Rooms[i].roomNo);
         roomForm.append("features_mask",featureVal);
         roomForm.append("mon_hrs",RoomAvail.mon);
         roomForm.append("tue_hrs",RoomAvail.tue);
         roomForm.append("wed_hrs",RoomAvail.wed);
         roomForm.append("thu_hrs",RoomAvail.thu);
         roomForm.append("fri_hrs",RoomAvail.fri);
         roomForm.append("sat_hrs",RoomAvail.sat);
         roomForm.append("sun_hrs",RoomAvail.sun);
         roomForm.append("area_dim",$scope.Rooms[i].area_dim);
         roomForm.append("prop_image",document.getElementById('prop_image').files[0]);
         roomForm.append("property_id",$scope.property_id);


         console.log("before");

  $.ajax({
           url:"http://localhost:1337/createRoom",
           type:"POST",
           method: "POST",
           mimeType: "multipart/form-data",
           // contentType: 'multipart/form-data',

           data: roomForm,
           success: (response)=>
           {
               console.log("success");
           },
           error: (error)=>
           {
             Swal.fire({
                   icon:'error',
                   title:'Oops...',
                   text: "Error in Query. Please contact System Admin."
                 });
           }
         })




nodejs ( I am trying to use moduler) if your prefer to use another library or so just tell me how :) :
app.post("/createRoom",upload.single("prop_image"), (req,res,next) =>{ 
      
    log("in create rooms"); 
    log(req.file); // CANNOT GET FILE!
    connection.query("INSERT INTO ......"
    ,(error,rows,field)=>
    {
        if(!!error)
        {
            res.status(400).send("Error in query :",error);
        }else
        {
            res.status(200).send("Rooms Created");
        }
    })
})


What I have tried:

i tried to pass FormData() and append the image to it but it is not working, illegal invocation error still pops up. I appreciate your work as this is taking me some time :(
Posted
Updated 3-Nov-20 0:51am
v2
Comments
Richard Deeming 3-Nov-20 4:50am    
You can't pass a plain JSON object to upload a file. You have to use FormData.
Using FormData Objects - Web APIs | MDN[^]

Update your question to show the FormData version of your code, and include the full details of the error.
Joe Doe234 3-Nov-20 4:57am    
I updated my question but still the same error ( illegal invocation)

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900