PHP前端开发

uniapp如何上传文件

百变鹏仔 2个月前 (11-21) #uniapp
文章标签 上传文件
uniapp上传文件的方法:嵌入h5页面,采用【web-view】标签,代码为【】。

本教程操作环境:windows7系统、uni-app2.5.1版本,Dell G3电脑。

推荐(免费):uni-app开发教程

uniapp上传文件的方法:

嵌入H5页面,需要采用web-view标签,如下:

<web-view></web-view>

注意:

  • h5页面必须在项目目录:/hybrid/html/下面,因为这样uni-app才不会进行编译

  • @message事件是h5页面向应用发送数据的回调

h5页面代码:

nbsp;html&gt;            <meta>        <meta>        <meta>        <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;        }    </style>                <div>            <form>                <a>选择文件                    <input>                </a>            </form>            <p></p>        </div>        <div>            <button>确定</button>            <button>取消上传</button>        </div>                <script></script>        <script></script>        <script>            $(".file").on("change", "input[type=&#39;file&#39;]", 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("");                }            });            $(&#39;.btn&#39;).click(function(evt) {                var formdata = new FormData(); // 创建一个form类型的数据                formdata.append("files",$("#uploadFile")[0].files[0]); // 获取上传文件的数据                formdata.append("operate","UpLoadFile"); // 获取上传文件的数据                formdata.append("name","name"); // 获取上传文件的数据                $.ajax({                    url: &#39;http://47.97.163.146:8080/Controler.ashx&#39;,                    type: "POST",                    processData: false,                    contentType: false,                    data:formdata,                    success: function(data) {                        // debugger                        console.log("这是请求成功的");                    },                    error: function(err) {                        console.log("这是请求失败的");                    }                });                var target = evt.target;                if (target.tagName === &#39;BUTTON&#39;) {                    var action = target.getAttribute(&#39;data-action&#39;);                    if (action == &#39;redirectTo&#39;) {                        uni.redirectTo({                            /* url: &#39;/pages/component/index&#39;, */                            url: &#39;/pages/index/index&#39;,                            success:function (d) {                                console.log("跳转成功");                            },                            fail:function(e){                                console.log(e);                            },                        });                    }                }            });                        //取消文件上传            $(&#39;.btn1&#39;).click(function(evt) {                var target = evt.target;                if (target.tagName === &#39;BUTTON&#39;) {                    var action = target.getAttribute(&#39;data-action&#39;);                    if (action == &#39;navigateBack&#39;) {                        uni.navigateBack({                            delta: 1                        });                    }                }            });                                </script>    

相关免费学习推荐:编程视频