ajax jQuery 实现文件上传
1.上传jsp。
<table cellpadding="0" cellspacing="0" border="0" class="ktable_9" >
<tr>
<td><input type="file" name="upoadFileName" id="upoadFileName"><a href="#" onclick="uploadFile();return false;"><span><span>上传</span></span></a></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
2.下面是前端的js脚本:
function uploadFile(){
var myurl="/uploadMyFile.do?tenderInfoId="+tenderInfoId;
jQuery.ajaxFileUpload(
{
url:myurl,
secureuri:false,
fileElementId:""upoadFileName"", --上传文件对应的文本域的id
dataType: ""json"",
success: function (data, status)
{
alert(""上传成功"");
},
error: function (data, status, e)
{
alert(""上传失败"");
}
} );
}
3.这是后台的action:
public ActionForward uploadMyFile(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
/* =============禁止缓存============== */
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Expires", "0");