我是靠谱客的博主 标致面包,这篇文章主要介绍uniapp如何上传文件,现在分享给大家,希望可以做个参考。

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

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

uniapp上传文件的方法:

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

复制代码
1
<web-view src="/hybrid/html/index.html" @message="handleMessage"></web-view>
登录后复制

注意:

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

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

h5页面代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!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='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 formdata = new FormData(); // 创建一个form类型的数据 formdata.append("files",$("#uploadFile")[0].files[0]); // 获取上传文件的数据 formdata.append("operate","UpLoadFile"); // 获取上传文件的数据 formdata.append("name","name"); // 获取上传文件的数据 $.ajax({ url: 'http://47.97.163.146:8080/Controler.ashx', 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 === 'BUTTON') { var action = target.getAttribute('data-action'); if (action == 'redirectTo') { uni.redirectTo({ /* url: '/pages/component/index', */ 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>
登录后复制

以上就是uniapp如何上传文件的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是标致面包最近收集整理的关于uniapp如何上传文件的全部内容,更多相关uniapp如何上传文件内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(524)

评论列表共有 0 条评论

立即
投稿
返回
顶部