html - How to upload files to database along with other form attributes using jquery and servlets -
this question has answer here:
<div class="modal fade" id="mymodal4" tabindex="-1" role="dialog" aria-labelledby="mymodallabel" aria-hidden="true"> <div class="modal-dialog" style="width: 40%;"> <!-- modal content--> <div class="modal-content"> <!-- modal header--> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="close"> <span aria-hidden="true">×</span> </button> <h4 class="modal-title" id="mymodallabel4">tender documents</h4> </div> <!-- modal body--> <div class="modal-body"> <div class="panel-body"> <form id="fileinfo" name="fileinfo" method="post"> <div> <label>upload file</label><br /> <input type="file" name="file" size="50" id="file"> </div> <div class="form-group"> <label>document serial no</label><br /> <input type="text" name="serialno" id="dserialno" class="form-control input-sm" placeholder="serial no" required="required"> </div> <div class="form-group"> <label>attach name</label><br /> <input type="text" name="attachname" id="dattachname" class="form-control input-sm" placeholder="attach name" required="required"> </div> <div class="form-group"> <label>description</label> <input type="text" id="ddesc" name="ddescription" /> </div> <!-- modal footer --> <div class="modal-footer"> <button type="button" class="btn btn-info" data-dismiss="modal" id="uploaddocument">submit</button> <button type="button" class="btn btn-info" data-dismiss="modal">cancel</button> </div> </form> </div> </div> <!-- modal body--> </div> <!-- modal content--> </div> </div> <!-- modal 4-->
the above code snippet form contains form , used uploading file , contains few attributes.
$(document).on('click','#uploaddocument', function(){ alert("uploaddocument"); var fd = new formdata(document.getelementbyid("fileinfo")); fd.append("label", "webupload"); var file=$('#file'); console.log(file); var id=$('.selected').attr('id'); alert(id); var obj={}; obj.id=id; obj.file=$('#file').val(); obj.serialno=($('#dserialno').val()).tostring(); obj.attachname=$('#dattachname').val(); obj.udescription=$('#ddesc').val(); $.ajax({ url: '/otherapplication/upload.do?id='+id, type: 'post', datatype: 'json', enctype: 'multipart/form-data', processdata: false, contenttype: false , data: json.stringify(obj), success: function( data, textstatus, jqxhr) { alert("success"); console.log(data); }, error: function(jqxhr, textstatus, errorthrown){ console.log("something bad happened " + textstatus); } }); });
the above code snippet jquery-ajax call how send form's data servlet can access file contents other attributes related form.in servlet not able access file contents other form attributes needs uploaded database.please provide me solution . in advance.
first need add enctype="multipart/form-data" in form tag attribute.
hope have added in same in ajax call take if form have.
normally using ajaxsubmit api handle form data servlet or webservice give objects listed in form on save or update.
good luck
please don't forgot use
<form id="data" method="post" enctype="multipart/form-data">.
Comments
Post a Comment