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>样式图:

uni-app实现文件上传(h5方式)的更多相关文章
- python_way day21 Django文件上传Form方式提交,原生Ajax提交字符处啊,Django文件上传之原生Ajax方式、jQuery Ajax方式、iframe方式,Django验证码,抽屉示例,
python_way day21 1.Django文件上传至Form方式 2.原生Ajax文件上传提交表单 使用原生Ajax好处:不依赖jquery,在发送一个很小的文件或者字符串的时候就可以用原生A ...
- 表单文件上传编码方式(enctype 属性)
enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码. 如下: <form action="upload.php" method="post&quo ...
- JavaWeb 文件上传 commons_fileupload方式
import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadExcept ...
- type=file文件上传H5新特性
1.语法 <input name="myFile" type="file"> 2.属性(以下三个仅HTML5支持,因此存在兼容性问题) (1)mul ...
- springMVC两种方式实现多文件上传及效率比较
springMVC实现 多文件上传的方式有两种,一种是我们经常使用的以字节流的方式进行文件上传,另外一种是使用springMVC包装好的解析器进行上传.这两种方式对于实 现多文件上传效率上却有着很大的 ...
- Java实现文件上传
最近自己在做一个小系统玩的时候涉及到了文件的上传,于是在网上找到Java上传文件的方案,最后确定使用common-fileupload实现上传操作. 需求说明 用户添加页面有一个“上传”按钮,点击按钮 ...
- java常见3种文件上传速度对比和文件上传方法详细代码
在java里面文件上传的方式很多,最简单的依然是FileInputStream.FileOutputStream了,在这里我列举3种常见的文件上传方法代码,并比较他们的上传速度(由于代码是在本地测试, ...
- servlet3.0文件上传与下载
描述:文件上传与下载是在JavaEE中常见的功能,实现文件上传与下载的方式有多种,其中文件上传的方式有: (1)commons-fileupload: (2)Servlet 3.0 实现文件上传 (3 ...
- Selenium2学习-039-WebUI自动化实战实例-文件上传下载
通常在 WebUI 自动化测试过程中必然会涉及到文件上传的自动化测试需求,而开发在进行相应的技术实现是不同的,粗略可划分为两类:input标签类(类型为file)和非input标签类(例如:div.a ...
随机推荐
- android activity 启动过程分析(source code 4.4)
说实话,android source code从2.3到4.4变化是蛮多的,尤其是media部分,虽然总的框架是没有多大变化,但是找起代码来看还是挺麻烦的.在android里面最受伤的是使用了java ...
- Ubuntu日常使用总结
Contents 使用了将近一年的Ubuntu,感觉不用windows也可以处理日常的事务.并且我相信只要合理利用Ubuntu,一定可以取代你手中的Windows.我不是说Ubuntu有多么好,只是从 ...
- Webpack 常用 modules
@(Javascript)[webpack] babel babel-core: babel 核心程式,知道如何載入程式碼.解析和輸出檔案(但不包含編譯). babel-loader: 用來告訴 ba ...
- 基本类型和引用类型的值 [重温JavaScript基础(一)]
前言: JavaScript 的变量与其他语言的变量有很大区别.JavaScript 变量松散类型的本质,决定了它只是在特定时间用于保存特定值的一个名字而已.由于不存在定义某个变量必须要保存何种数据类 ...
- 基于Blazor写一个简单的五子棋游戏
写这个五子棋游戏,其实主要目的是想尝试一下微软新作Blazor.Blazor对于那些搞.NET的程序员,又想做一些前端工作,真的挺友好,不用一句JS就可搞定前端交互,美哉.现在已经有很流行的前端框架, ...
- Java Web环境配置
准备工作 jdk-8u241 apache-tomcat-9.0.31-windows-x64.zip Eclipse IDE for Enterprise Java Developers 关于版本选 ...
- PHP压缩文件夹 php
$path = PUBLIC_DIR.'/images/'; //待压缩文件夹父目录 $zipPath = PUBLIC_DIR.'/images_zip/'; //压缩文件保存目录 !is_dir( ...
- ArrayList集合不能使用foreach增加、删除、修改元素的原因
大家先看两段代码 第一段代码: List<String> arrayList1 = new ArrayList<String>(); arrayList1.add(" ...
- czC#02
1.out参数 out参数要求在方法的内部必须为其赋值 using System; using System.Text; namespace Demo { class Program { //返回一个 ...
- 前端性能优化之Lazyload
前端性能优化之Lazyload @(Mob前端-冬晨)[JavaScript|技术分享|懒加载] [TOC] Lazyload 简介 前端工作中,界面和效果正在变得越来越狂拽炫酷,与此同时性能也是不得 ...