首頁 > web前端 > js教程 > 用jQuery外掛實現無刷新上傳檔案程式碼分享

用jQuery外掛實現無刷新上傳檔案程式碼分享

零下一度
發布: 2018-05-24 09:56:52
原創
1563 人瀏覽過

專案中會常用到AJAX無刷新上傳圖片,但是iframe上傳和flash插件都是比較複雜的,所以就找了一個jquery的插件。以下透過實例程式碼跟大家介紹使用jQuery ajaxupload外掛實現無刷新上傳檔案功能,需要的朋友參考下吧

專案中會常用到AJAX無刷新上傳圖片,但是iframe上傳和flash外掛都是比較複雜的,所以就找了一個jquery的插件。

程式碼如下

使用方法如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

<script type="text/javascript">

$(function () {

var button = $(&#39;#upload&#39;);

new AjaxUpload(button, {

action: &#39;/upload/imagesAjaxUpload&#39;,

name: &#39;upload&#39;,

onSubmit: function (file, ext) {

if (!(ext && /^(jpg|jpeg|JPG|JPEG)$/.test(ext))) {

alert(&#39;图片格式不正确,请选择 jpg 格式的文件!&#39;, &#39;系统提示&#39;);

return false;

}

// change button text, when user selects file

button.text(&#39;上传中&#39;);

// If you want to allow uploading only 1 file at time,

// you can disable upload button

this.disable();

// Uploding -> Uploading. -> Uploading...

interval = window.setInterval(function () {

var text = button.text();

if (text.length < 10) {

button.text(text + &#39;...&#39;);

} else {

button.text(&#39;上传中&#39;);

}

}, 200);

},

onComplete: function (file, response) {

window.clearInterval(interval);

// enable upload button

this.enable();

var json = eval(&#39;(&#39; + response + &#39;)&#39;);

button.text(&#39;选择文件&#39;);

$(".qr").css("display","inline");

$(".qr>img").attr("src",json.file_name);

$("input[name=&#39;wechat_qr&#39;]").val(&#39;/uploads/qr/&#39;+json.file_name);

//alert(json.file_name);

//$("#ajaximg").html("<img src=&#39;/uploads/qr/"+json.file_name+"&#39; />");

//$("#wechat_qr").val(&#39;/uploads/qr/&#39;+json.file_name);

}

});

});

</script>

登入後複製

以上是用jQuery外掛實現無刷新上傳檔案程式碼分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板