67,512
社区成员
发帖
与我相关
我的任务
分享
index.jsp -- 页面
<script>
//上传文件
function fileSub(){
var filePath = $("#filePath").val();
$.ajaxFileUpload(
{
url:'billfile_upload.jsp',
secureuri:false,
fileElementId:'filePath',
asynchronous:true,
dataType: 'json',
success: function (data){//没有运行下面的方法
alert("上传成功1111111111");
},
error:function(xml) {//却运行error中,并且alert中的信息为[object].[object]
alert(xml);
}
}
);
return false;
}
</script>
<form enctype="MULTIPART/FORM-DATA" name="upload" id="upload">
<tr>
<td width="30%">选择对账单:</td>
<td width="70%">
<input type="file" name="filePath" id="filePath"/>
<input type="button" name="fsub" id="fsub" onclick="return fileSub();" value="上传附件" style="width:80px"/>
</td>
</tr>
</form>
billfile_upload.jsp -- 文件上传处理jsp
String path = request.getContextPath();
String para = null;
FileHttpServletRequest req = FileRequestHandler.fileParse(request);
for (Iterator it = req.getParameterMap().entrySet().iterator(); it
.hasNext();) {
Entry en = (Entry) it.next();
para = (String) en.getKey();
}
FileItem item = req.getFileParameter(para);
String filePath = item.getName();
String extFile = filePath.substring(filePath.lastIndexOf("."));
String fname = Long.toString(new Date().getTime());
String dir = "..//webapps//" + path + "//UP//"
+ fname + extFile;
try{
item.write(new File(dir));//文件写入成功
out.print(fname + extFile);//此句正确执行,并且返回有值
} catch(Exception e) {
e.printStackTrace();
out.print("0");
}
success: function(data, status)
{
if(typeof(data.error) != 'undefined')
{
if(data.error!= '')
{
alert(data.error);
}else
{
alert(data.msg);
}
}