html:
<input type="text" id="f_soundfile" name="f_soundfile" value="">
<input type="file" id="f_soundfile2" name="f_soundfile2" value="${userdef.FSoundfile }">
js:
var _max_file_size = 15;//MB
var _allow_file_type = [];
function fileSelectErr(ev,info){
if(info.error_count['ext'] || info.error_count['mime'])
msg = "不安全的文件类型,请重新选择。";
if(info.error_count['size'])
msg = "附件大小超过限制的"+_max_file_size+"MB,请重新选择。";
alertSaveMsg(msg,"d");
}
$('#f_soundfile2').ace_file_input({
no_file:'未选择文件 ...',
btn_choose:'浏览',
btn_change:'修改',
droppable:true,
onchange:function(){
},
maxSize: _max_file_size*1024*1024,
thumbnail:false,
allowExt: _allow_file_type,
blacklist:'exe|jsp|html'
}).on("file.error.ace",fileSelectErr).on('change',function(){
var file = this.files[0];//获取文件
var reader = new FileReader();
reader.readAsDataURL(file);//调用自带方法进行转换
reader.onload = function(){//文件的base64位值
//var base64Data = this.result;
var base64Sound = this.result.split(",");//base64逗号前面的内容不是文件内容,去掉即可
$("#f_soundfile").val(base64Sound[1]);
}
});
java:
//执行保存
path = path + user.getFLogin() + ".mp3";// 存入服务器的最终路径(音频路径+文件名)
String soundfile = request.getParameter("f_soundfile");//base64格式文件
if (soundfile != null && !"".equals(soundfile)) {
File file2 = new File(path);
FileOutputStream out = new FileOutputStream(file2);
out.write(new BASE64Decoder().decodeBuffer(soundfile));//将文件写入到指定的服务器路径下
out.close();
}
注意:文件如果太大,base64可能会自动截取文件内容,导致附件上传不全!