2016-04-19 3 views
0

私はAjaxのやり取りを通してファイルをアップロードしています。私は、一緒に、サーバー側でパスを構築するための情報を送信したいと思います。しかし、私はそれが可能かどうか、私はそれをどうすることができるかは分かりません。他のデータをajaxのファイルと一緒に送ることは可能ですか?

function uploadFD(){ 

    var oMyForm = new FormData(); 

    jQuery.each(jQuery('#file')[0].files, function(i, file) { 
     oMyForm.append('file-'+i, file); 
    }); 

    $.ajax({ 
     url: 'FileController', 
     data: oMyForm, 
     processData: false, 
     contentType: false, 
     type: 'POST', 
     success: function(data){ 
       $('#Result').html(data.msg); 
       $.messager.show({ 
        title : 'Success', 
        msg : data.msg, 
        showType : 'show' 
       }); 
     } 
    }); 
} 

コントローラー:

@RequestMapping(method = RequestMethod.POST, headers = "content-type=multipart/*") 
    public void upload(MultipartHttpServletRequest request, 
      HttpServletResponse response) { 

    response.setCharacterEncoding("UTF-8"); 

    Iterator<String> itr = request.getFileNames(); 

    MultipartFile mpf = request.getFile(itr.next()); 
    System.out.println(mpf.getOriginalFilename() +" uploaded!"); 
    //... other stuff 
} 

私はそれをどのように行うことができますか?ありがとう!

+1

あなたが 'oMyForm.appendを()'を使用することができますフォームデータに他のパラメータを追加します。 – Barmar

答えて

1

はい、可能です。 FormData.appendへ 2番目のパラメータは、単純な文字列にすることができます。

oMyForm.append('param', 'value'); 

、あなたはrequest.getParameter()経由でサーバー上のパラメータの値にアクセスすることができます。

String v = request.getParameter("param"); 
+0

良い!ありがとう! – Alex

関連する問題