ajax上傳檔案

來源:互聯網
上載者:User

標籤:

之前用的ajax都是用來傳json格式的資料,用ajax傳檔案還是第一次,因為這個也是第一次嘗試,不知道這個方法是不是最好的,如果之後有更好的再進行修改吧。

我找的時候沒有在原生的ajax找到,找到了一個上傳檔案的外掛程式,就是一個封裝的ajax的方法,利用這個就能實現ajax上傳檔案。

下面是總結的一點點代碼。

$.ajaxFileUpload({            url: ‘<%=basePath %>drawingUpload/uploadOneFile.htm‘,             type: ‘post‘,            secureuri: false, //一般設定為false            fileElementId: ‘files‘, // 上傳檔案的id、name屬性名稱            dataType: ‘text‘, //傳回值類型,一般設定為json、application/json            data: {                 drawingCatalogId: drawingCatalogId,                 documentNumber: documentNumber,                 documentName: documentName,                remark: remark            },            success: function(data, status){                                  data = data.replace("<PRE>", ‘‘);  //ajaxFileUpload會對伺服器響應回來的text內容加上<pre>text</pre>前尾碼            data = data.replace("</PRE>", ‘‘);            data = data.replace("<pre>", ‘‘);            data = data.replace("</pre>", ‘‘); //本例中設定上傳檔案完畢後,服務端會返回給前台[0`filepath]            //將String字串轉換成json            var dataset = $.parseJSON(data);                        alert(dataset.message);            window.location.href = "drawingUpload/show.htm";            },            error: function(data, status, e){                 //alert(e);            }        });

 

<input id="files" name="files" type="file">

<script type="text/javascript" src="<%=path%>/resources/js/ajaxfileupload.js"></script>


    @RequestMapping(value="/uploadOneFile.htm", method=RequestMethod.POST)
    public @ResponseBody JSONObject uploadOneFile(HttpServletRequest request,
            int drawingCatalogId,String documentNumber,String documentName,String remark,MultipartFile files)
    {

//TODO

}

備忘:測試發現,ajaxfileupload對jQuery版本是有要求的,在使用中ajaxfileupload和jQuery對應的js版本要一致,不然會導致異常發生,可以從ajaxfileupload官網下載,避免版本衝突。

ajax上傳檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.