日期:2014-05-16  浏览次数:20660 次

jquery+struts1.3.8的ajax上传文件
最近做项目,对用户体验要求较高,大量采用jquery的ajax支持,最近要做个文件上传,要求也用ajax方式实现,这里采用了jquery的ajaxfileupload.js插件。见附件

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>&nbsp;</td>
            </tr>
        </table>

2.下面是前端的js脚本:

function uploadFile(){
                var myurl="/uploadMyFile.do?tenderInfoId="+tenderInfoId;
         jQuery.ajaxFileUpload(
                   {
                url:myurl,          
                secureuri:false,
                            fileElementId:'imgFile',   --上传文件对应的文本域的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");