这就是我最后所做的。
HTML格式:
<div class="form-group">
<embed width="191" height="207" id="image" src="" type="application/pdf" class="img-thumbnail">
<input class="form-control-file col-lg-12 col-md-12 col-sm-12 col-xs-12 photo-input" type="file" id="photo" name="photo" placeholder="PDF">
</div>
查询:
$(document).on('change', '.photo-input', function(){
//Check for a valid image extension
var img1 = this.files[0].type;
var mySubString = img1.substring(
img1.lastIndexOf("image") + 13
);
if($.inArray(mySubString, ['pdf']) == -1) {
alert('invalid extension!');
$('#image').attr('src', 'data:application\/(pdf);base64');
}else{
//Check for a valid image size
if (this.files[0].size < 10000000){
readURL(this, this.id);
}else{
alert("This image is to large (must be < 1 MB).")
$('#image').attr('src', 'data:application\/(pdf);base64');
}
var img1 = document.getElementById('image');
img2 = (img1.getAttribute('src')).replace(/^data:application\/(pdf);base64,/, "");
}
});
function readURL(input, id) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#image').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
submitHandler : function(showMPAddForm) {
var img1 = document.getElementById('image');
img2 = (img1.getAttribute('src')).replace(/^data:application\/(pdf);base64,/, "");
$.ajax({
type: "POST",
url: "MedicalPlanAddView",
cache: false,
data : {
ssAccountLevel : sessionStorage.getItem('ssAccountLevel'),
ssAccountID : sessionStorage.getItem('ssAccountID'),
ssNameID : sessionStorage.getItem('ssNameID'),
image : img2,
mpNameAdd: $("#mpNameAdd").val(),
},
})
.fail (function(jqXHR, textStatus, errorThrown) {
//alert(jqXHR.responseText);
$('#ajaxGetUserServletResponse13').text('Error adding Medical Plan.');
})
.done(function(responseJson){
$('#ajaxGetUserServletResponse13').text('Medical Plan added.');
showActionPlanDataTable();
})
}
SQL语言:
ps = c.prepareStatement(updateWithPhoto);
ps.setString(1, medicalPlanName);
BASE64Decoder decoder = new BASE64Decoder();
byte[] imageByte = decoder.decodeBuffer(medicalPlan);
ps.setBlob(2, new SerialBlob(imageByte));
ps.setString(3, updateDate);
ps.setString(4, medicalPlanId);
ps.executeUpdate();