【问题标题】:File upload struts 2 with Ajax使用 Ajax 上传文件的 struts 2
【发布时间】:2012-03-02 13:44:56
【问题描述】:

我正在使用带有 jsp 作为前端的 struts 2 上传文件,但我不想在文件上传后刷新页面,所以我使用的是 Ajax,但这样我无法获取 File 对象,似乎文件上传需要jsp中的表单标签,如果我提交表单,则页面会刷新。 我通过网络进行了研究,但无法获得很多相关结果,如果有人指导我完成这将有很大帮助,有没有办法。任何帮助将不胜感激。 最好的问候

【问题讨论】:

  • 试过这个例子FileUploadUsingAjax
  • 这些是jar包和zip包,有什么需要做的,能不能请指导我修改什么流程?
  • @EricIlavia 上面的链接是一个好的开始,它建议使用 json 插件,虽然它不是 100% 必需的,但它是最好的。该问题主要是客户端考虑使用经过验证的解决方案,例如:blueimp.github.com/jQuery-File-Upload 这是一个相当不错的 jQuery 文件上传插件。
  • 感谢你们的所有帮助,@Umesh Awashthi,非常感谢你们

标签: struts2 struts


【解决方案1】:

我建议使用 iframe 代替 ajax 来上传文件,

使用 struts2 和 iframe 上传 Csv 文件的示例代码:

    var file = $("#fileUpload").val();
    if(file.indexOf(".") != -1 && file.substr(file.indexOf("."))==".csv"){
        /* created  IFrame  For UPload file*/ 
        var iframe = $('<iframe name="uploadIPAddressIFrame"  id="uploadIPAddressIFrame" style="display: none" />');
        $("body").append(iframe);

        /* Set Form for submit iframe*/
        var form = $('#ipPoolForm');
        form.attr("action", "uploadCSVFile.do");
        form.attr("target", "uploadIPAddressIFrame");
        form.submit();

        openDialog(title);
        /* handle response of iframe */
        $("#uploadIPAddressIFrame").load(function () {
            response = $("#uploadIPAddressIFrame")[0].contentWindow.document.body.innerHTML;
            $("#chkIPAddressDiv").html(response);
            $("iframe#uploadIPAddressIFrame").remove();
        });     

如果您提交表单,则上传后更改表单目标:

// Because of  using iframe for upload set target value
$("#ipPoolForm").attr("target", "");

【讨论】:

    猜你喜欢
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多