最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jquery?ajax實現(xiàn)文件上傳提交的實戰(zhàn)步驟

 更新時間:2022年11月16日 11:16:42   作者:堅毅的小解同志  
今天項目中加了一個上傳文件的需求,我就搞了一下,下面這篇文章主要給大家介紹了關(guān)于jquery?ajax實現(xiàn)文件上傳提交的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下

在這里看原生ajax實現(xiàn)文件上傳

JQuery實現(xiàn)文件上傳提交  

定義UI結(jié)構(gòu)

 <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>
 <input type="file" id="file1">
    <button id="btnUpload">上傳文件</button>
     <img src="" alt="" style="width: 200px;" id="img1">

驗證是否選擇了文件

        $('#btnUpload').on('click', function () {
            let files = $('#file1')[0].files;
            if (files.length <= 0) {
                return alert('請選擇文件后在上傳')
            }
        
        })

向FormData中追加文件并發(fā)起ajax請求

           //上傳文件
            let fd = new FormData();
            fd.append('avator', files[0]);

            //發(fā)起jquery  ajax請求
                       $.ajax({
                method: 'post',
                url: 'http://www.liulongbin.top:3006/api/upload/avatar',
                data: fd,
                processData: false,
                contentType: false,
                success: function (res) {
                    alert('上傳成功')
                    $('#img1').attr('src', 'http://www.liulongbin.top:3006' + res.url)
                    console.log(res.url);
                }
            })

 jquery實現(xiàn)loading效果

ajaxStart(callback)
Ajax請求開始時,執(zhí)行ajaxStart函數(shù),可以在ajaxStart的callback中顯示loading效果。
自jqueyr版本1.8起,該方法只能被附加到文檔,$(document).ajaxStart()函數(shù)會監(jiān)聽文檔內(nèi)所有ajax請求,當(dāng)ajax請求開始會觸發(fā)這個函數(shù),ajax結(jié)束則會觸發(fā)ajaxStop

  <img src="./自媒體資源/5-121204193933-51.gif" alt="" style="display: none;" id="loading" width="50px" height="50px">
          $(document).ajaxStart(function () {
            $('#loading').show()
        })
        $(document).ajaxStop(function () {
            $('#loading').hide()
        })

完整代碼

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>

</head>

<body>
    <input type="file" id="file1">
    <button id="btnUpload">上傳文件</button>
    <br>
      <img src="" alt="" style="width: 200px;" id="img1">
    <img src="./自媒體資源/5-121204193933-51.gif" alt="" style="display: none;" id="loading" width="50px" height="50px">
    <script>
        //監(jiān)聽傳輸
        $(document).ajaxStart(function () {
            $('#loading').show()
        })
        $(document).ajaxStop(function () {
            $('#loading').hide()
        })

        //建立單擊事件
        $('#btnUpload').on('click', function () {
            let files = $('#file1')[0].files;
            if (files.length <= 0) {
                return alert('請選擇文件后在上傳')
            }
            //上傳文件
            let fd = new FormData();
            fd.append('avator', files[0]);

            //發(fā)起jquery  ajax請求
            $.ajax({
                method: 'post',
                url: 'http://www.liulongbin.top:3006/api/upload/avatar',
                data: fd,
                processData: false,
                contentType: false,
                success: function (res) {
                    alert('上傳成功')
                    $('#img1').attr('src', 'http://www.liulongbin.top:3006' + res.url)
                    console.log(res.url);
                }
            })
        })
    </script>
</body>

請?zhí)砑訄D片描述

補充:Jquery Ajax上傳文件并提交表單其它屬性

1.前端代碼

$('.btn-confirm').click(function () {
            console.log($( '#postForm').serialize());
 
            var versionNo = $("#versionNo").val();
            var deviceNoStr = $("input[name='deviceNoStr']").val();
            var batchId = $("input[name='batchId']").val();
 
            var formData = new FormData();
            console.log($('#upfile')[0].files[0]);
 
            formData.append("upfile",$('#upfile')[0].files[0]);
            formData.append("versionNo", versionNo);
            formData.append("deviceNoStr", deviceNoStr);
            formData.append("batchId", batchId);
 
            $.ajax({
                url :window.CMS_URL + "/devops/operation/upgrade.shtml",
                dataType:'json',
                type:'POST',
                async: false,
                data: formData,
                processData : false, // 使數(shù)據(jù)不做處理
                contentType : false, // 不要設(shè)置Content-Type請求頭
                success: function(data){
                    console.log(data);
                    if (data.status == 'ok') {
                        alert('上傳成功!');
                    }
 
                },
                error:function(response){
                    console.log(response);
                }
            });
 
        });
<form id= "uploadForm" method= "post" enctype ="multipart/form-data">
            <div class="portlet-body" style="margin: 15px 0;">
                <span class="file-des">程序路徑:</span>
                <div class="wrap-upload" style="display: flex">
                    <div class="showFileName"
                    ></div>
                    <a href="javascript:;" class="file btn btn-sm green">上傳文件
                        <input type="file" name="upfile" id="upfile" placeholder="請選擇文件">
                    </a>
                    <div class="fileerrorTip" style="line-height: 30px;margin-right: 10px"></div>
                </div>
            </div>
            <div class="portlet-body">
                <span class="file-des">版本號:</span>
                <div class="wrap-upload item-inputs" >
                    <input class="form-control" type="text" name="versionNo" id="versionNo" placeholder="版本號" required>
                </div>
            </div>
            <div style="    margin: 0px 100px;padding-top: 20px;">
                <div class="btn btn-sm green control-btn-item btn-confirm" >確認(rèn)</div>
                <div style="margin-left: 30px;" class="btn btn-sm green control-btn-item" onclick="closePopup()">取消</div>
            </div>
 
            <input type="hidden" name="deviceNoStr" value="${(devopsParamsQo.deviceNoStr)!''}"/>
            <input type="hidden" name="batchId" value="${(devopsParamsQo.batchId)!''}"/>
        </form>

2.后端代碼

 @RequestMapping(value = "/upgrade", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo upgrade(@RequestPart( value = "upfile", required = true) MultipartFile file,
                            @RequestParam(name = "versionNo", required = true) String versionNo,
                            @RequestParam(name = "batchId", required = true) String batchId,
                            @RequestParam(name = "deviceNoStr", required = true) String deviceNoStr) {
        DevopsParamsQo qo = new DevopsParamsQo();
        qo.setDeviceNoStr(deviceNoStr);
        List<DeviceDto> addList = getDeviceDtos(qo);
 
        // TODO 調(diào)用前置
        return ResultVoGenerator.genSuccessResultVo();
    }

總結(jié) 

到此這篇關(guān)于jquery ajax實現(xiàn)文件上傳提交的文章就介紹到這了,更多相關(guān)jquery ajax文件上傳提交內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • jQuery如何解決IE輸入框不能輸入的問題

    jQuery如何解決IE輸入框不能輸入的問題

    在IE10以上版本,微軟為了提高IE輸入框的便利性,增加了文本內(nèi)容全部刪除和密碼眼睛功能,但是有些時候打開新的頁面里,輸入框卻被鎖定無法編輯,需要刷新一下頁面,或者如果輸入框有內(nèi)容需要點擊一下輸入框后面的叉叉后才能輸入,怎么解決呢,下面小編給大家解答下
    2016-10-10
  • jQuery表單美化插件jqTransform使用詳解

    jQuery表單美化插件jqTransform使用詳解

    這里主要是jQuery美化部分的實例,美化對象為文本框、密碼框、文本域、Select下拉框、按鈕等,有了這些示例,美化其它的表單元素應(yīng)該不是大問題。
    2015-04-04
  • jQuery on()方法使用技巧詳解

    jQuery on()方法使用技巧詳解

    這篇文章主要介紹了jQuery on()方法使用技巧詳解,本文講解了trigger()綁定、多個事件綁定同一個函數(shù)、多個事件綁定不同函數(shù)、綁定自定義事件、傳遞數(shù)據(jù)到函數(shù)等技巧,需要的朋友可以參考下
    2015-04-04
  • jQuery實現(xiàn)表單步驟流程導(dǎo)航代碼分享

    jQuery實現(xiàn)表單步驟流程導(dǎo)航代碼分享

    這篇文章主要介紹了jQuery實現(xiàn)表單步驟流程導(dǎo)航,代碼實現(xiàn)效果簡單精致,推薦給大家,有需要的小伙伴可以參考下。
    2015-08-08
  • jQuery實現(xiàn)checkbox列表的全選、反選功能

    jQuery實現(xiàn)checkbox列表的全選、反選功能

    本文給大家分享的是一個jQuery實現(xiàn)checkbox列表的全選、反選功能的封裝代碼,非常的實用,有需要的小伙伴可以參考下
    2016-11-11
  • 關(guān)于 jQuery Easyui異步加載tree的問題解析

    關(guān)于 jQuery Easyui異步加載tree的問題解析

    想要實現(xiàn)從本地中加載json文件,通過事件來動態(tài)的插入到ul中時,遇到了一小bug,下面小編給大家解答下
    2016-12-12
  • jquery實現(xiàn)圖片平滑滾動詳解

    jquery實現(xiàn)圖片平滑滾動詳解

    這篇文章主要為大家詳細(xì)介紹了jquery實現(xiàn)圖片平滑滾動的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Jquery中的層次選擇器與find()的區(qū)別示例介紹

    Jquery中的層次選擇器與find()的區(qū)別示例介紹

    Jquery中的層次選擇器與find()是有一定的區(qū)別,本文有個示例可以很好的說明,感興趣的朋友可以參考下
    2014-02-02
  • jquery簡易手風(fēng)琴插件的封裝

    jquery簡易手風(fēng)琴插件的封裝

    這篇文章主要為大家詳細(xì)介紹了jquery簡易手風(fēng)琴插件的封裝,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • bootstrap與Jquery UI 按鈕樣式?jīng)_突的解決辦法

    bootstrap與Jquery UI 按鈕樣式?jīng)_突的解決辦法

    這篇文章主要介紹了bootstrap與Jquery UI 按鈕樣式?jīng)_突的解決辦法的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09

最新評論

安庆市| 衡南县| 玉龙| 安庆市| 旌德县| 永城市| 台江县| 天等县| 彭阳县| 汽车| 沽源县| 酉阳| 盱眙县| 玛沁县| 洛南县| 诸城市| 商南县| 兴城市| 望城县| 景德镇市| 达拉特旗| 都昌县| 柯坪县| 会同县| 镇巴县| 永登县| 深州市| 泗阳县| 郸城县| 汶川县| 姚安县| 合川市| 鹤峰县| 盐津县| 耒阳市| 芜湖县| 衡水市| 丹棱县| 会东县| 垦利县| 伽师县|