原生js實現簡單的單文件上傳操作


效果

核心思想

  1. 將原生的 <input type="file">隱藏起來,具體來說就是隱藏在【瀏覽按鈕下方】
  2. 點擊瀏覽按鈕時其實點擊的是下方的  <input type="file">;
  3. 將獲取到的文件名顯示在 <input type="text">的顯示欄中
  4. 使用FormData對象獲取文件數據;
  5. 通過設置ajax的參數,實現數據數據的上傳

html代碼(具體的業務代碼可以忽略)

<div id="scheduleFileUploadDiv">
    <div id="scheduleFileChooseDiv">
        <input id="scheduleFileNameInput" placeholder="請選擇排班文件" type="text" />
        <form action="\'+that.scheduleFileUploadUrl+\'" 
            id="scheduleFileUploadForm" enctype="multipart/form-data" style="display: inline-block; ">
            <input class="scheduleChooseFileInput" id="scheduleFile" type="file" name="file" />瀏覽
    </div>
    <div class="scheduleFileImportDialogFooterDiv">
        <button id="scheduleFileUploadBtn" onclick="scheduleImport.upload()">上傳</button>
        <label id="scheduleImportErrorMsgLabel" style="color: red"></label>
    </div>
</div>

css代碼

重點代碼加了背景色

#scheduleFileUploadDiv{
    width: 700px;
    height: 110px;
}

#scheduleFileChooseDiv{
    height: 50px;
    width: 98%;
    margin: 20px 0px 0px 20px;
}

.scheduleFileImportDialogFooterDiv {
    padding: 10px;
    margin-bottom: 10px;
}

.scheduleChooseFileInput{
    width: 97%;
    height: 28px;
    opacity: 0;
    display: inline-block;
    position: absolute;
    top: 0;
    left: 0;
    cursor: pointer;
}

#scheduleFileUploadForm{
    position: relative;
    cursor: pointer;
    color: #9bcbe6;
    font-size: 14px;
    font-weight: 700;
    line-height: 30px;
    text-align: center;
    border: 1px solid #1d5367;
    border-radius: 2px;
    /* overflow: hidden; */
    width: 100px;
    background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#254358), to(#0c293c));
    margin: 0 7px;
}

#scheduleFileNameInput{
    width: 500px;
    height: 30px;
    line-height: 30px;
    padding: 0 5px;
    border: 1px solid #9e9a9a;
    border-radius: 5px;
}

#scheduleFileUploadBtn{
    background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#65b0c7), to(#3d85a0));
    border: solid 1px #54a8c0;
    border-radius: 2px;
    color: #0c273a;
    width: 100px;
    font-size: 14px;
    font-weight: 700;
    line-height: 30px;
    margin-left: 280px;
}

js代碼

設置文件名顯示代碼,最好通過setInterval循環設置

refreshUploadFileName:function () {
    var fileInput = $("#scheduleFile").get(0).files[0];
    if(fileInput){
        $("#scheduleFileNameInput").val(fileInput.name);
    }
}

文件上傳代碼

upload:function(){
    var that = this;
    var file = document.getElementById("scheduleFile").files[0];
    var formData = new FormData();
    formData.append("file",file);
    $.ajax({
        type: 'POST',
        url: that.scheduleFileUploadUrl,
        data:formData,
        cache: false,                      // 不緩存
        processData: false,                // jQuery不要去處理發送的數據
        contentType: false,                // jQuery不要去設置Content-Type請求頭
        success: function (data){
            if(data["success"]){
            }else{
            }
        },
        error:function(){
        },
        dataType: "json",
        async: true
    });
}

 

 

參考文章:

https://segmentfault.com/a/1190000012327982?utm_source=tag-newest

https://www.cnblogs.com/Fancy1486450630/p/10948204.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM