uni-app實現文件上傳(h5方式)


1.嵌入H5頁面,需要采用web-view標簽,如下:

<web-view src="/hybrid/html/index.html" @message="handleMessage"></web-view>

注意:

  • h5頁面必須在項目目錄:/hybrid/html/下面,因為這樣uni-app才不會進行編譯
  • @message事件是h5頁面向應用發送數據的回調

     

     

     2.h5頁面代碼:

  • <!DOCTYPE html>
    <html lang="zh">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>上傳文件</title>
    <style>
    *{
    margin: 0;
    padding: 0;
    }
    .head-btn{
    text-align: center;
    margin-top: 50px;
    }
    .file {
    position: relative;
    display: inline-block;
    background: #D0EEFF;
    border: 1px solid #99D3F5;
    border-radius: 10px;
    padding: 24px 50px;
    overflow: hidden;
    color: #1E88C7;
    text-decoration: none;
    text-indent: 0;
    line-height: 20px;
    font-size: 40px;
    }
    .file input {
    position: absolute;
    font-size: 200px;
    right: 0;
    top: 0;
    opacity: 0;

    }
    .file:hover {
    background: #AADFFD;
    border-color: #78C3F3;
    color: #004974;
    text-decoration: none;
    }
    .determine{
    color: #FFFFFF;
    background-color: #007AFF;
    display: inline-block;
    font-size: 20px;
    border-radius: 5px;
    padding: 8px 24px;
    }
    .showFileName{
    display: inline-block;
    height: 30px;
    min-width: 300px;
    }
    .btn {
    display: block;
    margin: 20px auto;
    padding: 5px;
    background-color: #007aff;
    border: 0;
    color: #ffffff;
    height: 40px;
    width: 200px;
    border-radius: 5px;
    }
    .btn1 {
    display: block;
    margin: 20px auto;
    padding: 5px;
    background-color: #007aff;
    border: 0;
    color: #ffffff;
    height: 40px;
    width: 200px;
    border-radius: 5px;
    }

    .btn-red {
    background-color: #dd524d;
    }

    .btn-yellow {
    background-color: #f0ad4e;
    }

    .desc {
    padding: 10px;
    color: #999999;
    }
    .text{
    background-color: #007AFF;
    text-align: center;
    font-size: 18px;
    color: white;
    height: 45px;
    }
    .text-in{
    padding-top: 10px;
    }
    .body{
    background-image: url(../../static/timg.jpg);
    }

    </style>
    </head>
    <body class="body">
    <div class="text">
    <div class="text-in">
    文件上傳
    </div>
    </div>
    <div class="head-btn">
    <form action="" method="post">
    <a href="javascript:;" class="file">選擇文件
    <input type="file" name="uploadFile" id="uploadFile" >
    </a>
    </form>
    <p class="showFileName"></p>

    </div>
    <div>
    <button class="btn" type="button" data-action="redirectTo">確定</button>
    <button class="btn1" type="button" data-action="navigateBack">取消上傳</button>
    </div>

    <script src="./js/jQuery1_10_2.js"></script>
    <script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script>
    <script>
    console.log("轉檢編號:"+getQuery('id')); //轉檢編號
    //取url中的參數值
    function getQuery(id) {
    // 正則:[找尋'&' + 'url參數名字' = '值' + '&']('&'可以不存在)
    let reg = new RegExp("(^|&)"+ id +"=([^&]*)(&|$)");
    let r = window.location.search.substr(1).match(reg);
    //console.log("r="+r);
    if(r != null) {
    // 對參數值進行解碼
    return decodeURIComponent(r[2]);
    }
    return null;
    }

    $(".file").on("change", "input[type='file']", function() {
    let filePath = $(this).val();
    // console.log(filePath);
    localStorage.setItem("fileAddress", filePath);
    let lastname = localStorage.getItem("fileAddress");
    if (lastname != "") {
    $(".showFileName").html(lastname);
    } else {
    $(".showFileName").html("");
    }
    });
    $('.btn').click(function(evt) {
    var fileUrl;
    var formdata = new FormData(); // 創建一個form類型的數據
    formdata.append("files",$("#uploadFile")[0].files[0]); // 獲取上傳文件的數據
    formdata.append("operate","UpLoadFile"); // 操作碼
    formdata.append("name","name");
    //console.log("files:"+formdata.get("files") + ",name:" + formdata.get("name"))
    console.log("begin")
    $.ajax({
    url: 'http://47.97.163.146:8080/Controler.ashx',
    type: "POST",
    async:false, //同步請求,否則內部嵌套的ajax不會運行
    processData: false,
    contentType: false,
    data:formdata,
    dataType:"json",
    success: function(data) {
    //更新數據庫添入附件的url
    console.log("data:" + JSON.stringify(data))
    fileUrl = data.url;
    // var formdata1 = new FormData(); // 創建一個form類型的數據
    // formdata1.append("operate","UploadFileZJ"); // 操作碼
    // formdata1.append("ID",getQuery('id'));
    // formdata1.append("url",fileUrl);
    console.log("url地址:"+fileUrl);
    console.log("轉檢編號:"+getQuery('id'));
    //console.log("url:"+formdata1.get("url") + ",ID:" + formdata1.get("ID")) //查看url和轉檢id
    $.ajax({
    url: 'http://47.97.163.146:8080/Controler.ashx',
    type: "POST",
    data:{operate:"UploadFileZJ",ID:getQuery('id'),url:fileUrl},
    dataType:"json",
    success: function(data){
    console.log("更新成功")
    },
    error: function(err) {
    console.log(555)
    console.log("更新失敗的");
    }
    })
    console.log("這是請求成功的");
    },
    error: function(err) {

    console.log("這是請求失敗的");
    }
    });
    console.log("end")

    var target = evt.target;
    if (target.tagName === 'BUTTON') {
    var action = target.getAttribute('data-action');
    if (action == 'redirectTo') {
    uni.redirectTo({
    url: '/pages/index/index',
    success:function (d) {
    console.log("跳轉成功");
    },
    fail:function(e){
    console.log(e);
    },
    });
    }
    }
    });

    //取消文件上傳
    $('.btn1').click(function(evt) {
    var target = evt.target;
    if (target.tagName === 'BUTTON') {
    var action = target.getAttribute('data-action');
    if (action == 'navigateBack') {
    uni.navigateBack({
    delta: 1
    });
    }
    }
    });


    </script>
    </body>
    </html>

    樣式圖:

     

     


免責聲明!

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



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