欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

JQuery實(shí)現(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">

驗(yàn)證是否選擇了文件

        $('#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實(shí)現(xiàn)loading效果

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

補(bǔ)充: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">版本號(hào):</span>
                <div class="wrap-upload item-inputs" >
                    <input class="form-control" type="text" name="versionNo" id="versionNo" placeholder="版本號(hào)" 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實(shí)現(xiàn)文件上傳提交的文章就介紹到這了,更多相關(guān)jquery ajax文件上傳提交內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論