】."/> 】.">
search
HomeWeb Front-enduni-appHow to upload files in uniapp

How to upload files in uniapp

Dec 09, 2020 pm 03:53 PM
uniappupload files

Uniapp upload file method: embed H5 page, use [web-view] tag, the code is [].

How to upload files in uniapp

The operating environment of this tutorial: windows7 system, uni-app2.5.1 version, Dell G3 computer.

Recommended (free): uni-app development tutorial

Uniapp upload file method:

To embed an H5 page, you need to use the web-view tag, as follows:

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

Note:

  • ##h5 page It must be under the project directory:

    /hybrid/html/, because uni-app will not be compiled

  • @ The message event is the callback for the h5 page to send data to the application

h5 page code:


<!DOCTYPE html>
<html>
    <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;
        }
    </style>
    </head>
    <body>
        <div>
            <form action="" method="post">
                <a href="javascript:;">选择文件
                    <input type="file" name="uploadFile" id="uploadFile" >
                </a>
            </form>
            <p></p>
        </div>
        <div>
            <button type="button" data-action="redirectTo">确定</button>
            <button 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>
            $(".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>
    </body>
</html>

Related free learning recommendations:

programmingvideo

The above is the detailed content of How to upload files in uniapp. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you debug issues on different platforms (e.g., mobile, web)?How do you debug issues on different platforms (e.g., mobile, web)?Mar 27, 2025 pm 05:07 PM

The article discusses debugging strategies for mobile and web platforms, highlighting tools like Android Studio, Xcode, and Chrome DevTools, and techniques for consistent results across OS and performance optimization.

What debugging tools are available for UniApp development?What debugging tools are available for UniApp development?Mar 27, 2025 pm 05:05 PM

The article discusses debugging tools and best practices for UniApp development, focusing on tools like HBuilderX, WeChat Developer Tools, and Chrome DevTools.

How do you perform end-to-end testing for UniApp applications?How do you perform end-to-end testing for UniApp applications?Mar 27, 2025 pm 05:04 PM

The article discusses end-to-end testing for UniApp applications across multiple platforms. It covers defining test scenarios, choosing tools like Appium and Cypress, setting up environments, writing and running tests, analyzing results, and integrat

What are the different types of testing that you can perform in a UniApp application?What are the different types of testing that you can perform in a UniApp application?Mar 27, 2025 pm 04:59 PM

The article discusses various testing types for UniApp applications, including unit, integration, functional, UI/UX, performance, cross-platform, and security testing. It also covers ensuring cross-platform compatibility and recommends tools like Jes

What are some common performance anti-patterns in UniApp?What are some common performance anti-patterns in UniApp?Mar 27, 2025 pm 04:58 PM

The article discusses common performance anti-patterns in UniApp development, such as excessive global data use and inefficient data binding, and offers strategies to identify and mitigate these issues for better app performance.

How can you use profiling tools to identify performance bottlenecks in UniApp?How can you use profiling tools to identify performance bottlenecks in UniApp?Mar 27, 2025 pm 04:57 PM

The article discusses using profiling tools to identify and resolve performance bottlenecks in UniApp, focusing on setup, data analysis, and optimization.

How can you optimize network requests in UniApp?How can you optimize network requests in UniApp?Mar 27, 2025 pm 04:52 PM

The article discusses strategies for optimizing network requests in UniApp, focusing on reducing latency, implementing caching, and using monitoring tools to enhance application performance.

How can you optimize images for web performance in UniApp?How can you optimize images for web performance in UniApp?Mar 27, 2025 pm 04:50 PM

The article discusses optimizing images in UniApp for better web performance through compression, responsive design, lazy loading, caching, and using WebP format.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools