jquery - Upload image to mysql using jsp,servlet -


this question has answer here:

jsp

<form action="addcategoryservlet" id="addcategoryform" target="_self" method="post">         <table>         <tr>              <td> <div align="center" class="group">                  <input type="text" id="cname" name="texcname" required />                  <span class="highlight"></span>                  <span class="bar"></span>                  <label id="cnamecheck">category name</label>                  </div>         </tr>         <tr>             <td> <div align="center" class="group">                  <input type="text" id="cdescript" name="texcdescript" required />                  <span class="highlight"></span>                  <span class="bar"></span>                  <label id="cdescriptcheck">category description</label>                  </div>         </tr>          <tr>             <td> <div align="center" class="group">                  <input type="text" id="cimage" name="texcimage" required/>                  <span class="highlight"></span>                  <span class="bar"></span>                  <label id="cimagecheck">file path</label>                  <input style="display:none;" type="file" id="file" name="file"/>                  <input style="position:absolute;top:10px;left:315px;font-size:10px;" type="button" value="choose image" id="uploadbutton" class="mybutton" />                  </div>           </tr>                        </table>         <input type="submit" value="add category" class="mybutton" />         </form> 

choose image button when clicked fires click event input type=file using jquery such-:

$("#uploadbutton").click(function(){     $("#file").click(); }); 

my servlet putting file databse -:

try     {         string name,description=null;         name=request.getparameter("texcname");         description=request.getparameter("texcdescript");          inputstream inputstream= null;         part filepart= null;         filepart= request.getpart("file");          if (filepart != null)          {             system.out.println(filepart.getname());             system.out.println(filepart.getsize());             system.out.println(filepart.getcontenttype());              inputstream = filepart.getinputstream();         }          connection con= basedao.getconnection();          preparedstatement pst = con.preparestatement("insert category(category_name, image, description) values (?,?,?)");         pst.setstring(1,name);         pst.setblob(2,inputstream);         pst.setstring(3,description);         pst.executeupdate();       }     catch(classnotfoundexception | sqlexception e)     {         e.printstacktrace();     } 

i getting error of column 'image' cannot null. upon debugging found value of variable filepart null i.e. not receiving parameter name="file" jsp. can please point out i'm going wrong.

you cannot handle plain input types text , input type file @ time in single form, need special libraries handle such request.

you need write enctype="multipart/form-data" within form as,

 <form action="addcategoryservlet" id="addcategoryform" target="_self" method="post" enctype="multipart/form-data" >      ........  </form> 

however, note writing enctype="multipart/form-data" won't able handle plain input i.e. input type="text"

so if want simple approach create 2 seperate forms contains enctype="multipart/form-data" when send image , 1 have specified in question contains text data.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -