我使用summernote並希望異步服務器上傳能力。我打算圖像轉換爲Base64和發送通過Ajax與Servlet在那裏,我會保存壓縮文件並返回圖像文件的URL爲下面編輯器源:下面base64到圖像文件
var edit = function() {
$('.click2edit').summernote({
focus: true,
onImageUpload: function(files, editor, welEditable) {
sendFile(files[0],editor,welEditable);
}
});
};
function sendFile(file,editor,welEditable) {
alert(file.size);
var reader = new FileReader();
var imgfile = reader.readAsBinaryString(file);
alert(file);
$.ajax({
method:"POST",
url: 'imageupload',
data: {imageFile:imgfile},
success:function(response)
{
alert("file uploaded successfully");
return response;
},
error: function(response,status,err)
{
alert("upload failed");
}
});
}
是我的servlet代碼。在這裏我得到的文件爲空。我相信我應該把文件作爲字符串在這裏。有人可以幫忙嗎?
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("image upload");
String file = (String)request.getParameter("imageFile");
System.out.println("file: " + file);
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.print(file);
}