Angular 使用 ng2-file-upload 上传文件遇到的问题
Angular 上传文件 可参考Angular2使用ng2-file-upload上传文件
这里记录在开发过程中遇到的问题:
删除选择的文件后,不能再选择上次选择的相同的文件
在 firefox 浏览器中,当未选择文件时,原样式是:

当已选择文件时,原样式是:

根据设计要求,需要添加删除按钮,用于删除选择的文件。
在开发的过程中发现删除后,不能选择上一次选择的相同的文件,而其他的文件可以选择。
因为选择文件的时候是用的 (change)事件,所以在删除之后不能选择相同的文件。
我的方法是,删除之后清除掉上传的 input dom,然后再创建这个 dom。
模板中:
input 元素添加 *ngIf = "isShowSelectFile"
组件中:
初始化时,isShowSelectFile: boolean = true;
删除时:
this.isShowSelectFile = false;
setTimeout(() => {
this.isShowSelectFile = true;
}, 100);
这里附上关键代码,查看codepen在线示例关键代码(**ng2-file-upload 插件在 Angular 中怎么使用请参考Angular2使用ng2-file-upload上传文件 **):
<div class="upload-template">
<strong>上传模板:</strong>
<div class="upload-file-container">
<span class="upload-name" [class.uploaded-template]="selectedFileName !== '选择文件'">{{selectedFileName}}</span>
<input id="selectFile" type="file" *ngIf="isShowSelectFile" placeholder="选择填写好的数据文件" title="选择填写好的数据文件"
ng2FileSelect
[uploader]="uploader"
[disabled]="isImportingData"
accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
(change)="selectedFileOnChanged($event)">
</div>
<!--若文件已上传-->
<button [hidden]="selectedFileName == '选择文件'" type="button" class="delete" title="删除" (click)="deleteUploadedFile()">删除</button>
</div>
// 删除上传的文件
deleteUploadedFile() {
this.selectedFileName = "选择文件";
this.uploader.clearQueue();
this.uploadEnabled = false;
this.isShowSelectFile = false;
setTimeout(() => {
this.isShowSelectFile = true;
}, 100);
}
selectedFileOnChanged(event:any) {
let filePath = event.target.files[0].name;
//用户取消选择文件
if(event.target.value =="") {
this.uploader.clearQueue();
this.selectedFileName = "选择文件";
this.uploadEnabled = false;
} else {
//每次选择后,都只保留最新选择的文件
let fileCount = this.uploader.queue.length;
if(fileCount > 1) {
for(let i = 1; i < fileCount; i++) {
this.uploader.removeFromQueue(this.uploader.queue[0])
}
}
}
this.uploadEnabled = this.uploader.queue.length > 0;
}
SCSS:
.upload-template {
$size: 36px;
.upload-file-container {
position: relative;
height: $size;
line-height: $size;
cursor: pointer;
margin-left: 10px;
.upload-name {
z-index: 2;
width: 100%;
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
display: block;
text-decoration: underline;
height: $size;
line-height: $size;
color: #29e;
overflow-x: hidden;
text-overflow: ellipsis;
white-space: nowrap;
&.uploaded-template {
color: #444;
text-decoration: none;
}
& + input {
z-index: 3;
width: 100%;
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
display: block;
height: $size;
line-height: $size;
opacity: 0;
}
}
}
.delete {
float: left;
color: #f00;
line-height: $size;
padding-right: 1em;
padding-left: 1em;
}
}
在 firefox 浏览器中,当未选择文件时,美化后样式是:

当已选择文件时,美化后样式是:

Angular 使用 ng2-file-upload 上传文件遇到的问题的更多相关文章
- Struts Upload上传文件
1.Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.te ...
- php 下 html5 XHR2 + FormData + File API 上传文件
FormData的作用: FormData对象可以帮助我们自动的打包表单数据,通过XMLHttpRequest的send()方法来提交表单.当然FormData也可以动态的append数据.FormD ...
- element-ui upload上传文件并携带参数 使用formData对象
需求:上传文件的时候,需要携带其他的参数 问题:使用upload上传文件时,必须使用formData对象,而其他的参数通过data获取的到的,formData和data是不能同时传输的 解决:获取到的 ...
- thinkphp Upload上传文件在客户端生成的临时文件$_FILES['file']['tmp_name']
1.关于thinkphp 的Upload的$_FILES['file']['tmp_name'] 在使用thinkphp上传图片的时候,在上传的$_FILES数组中,有一个$_FILES['file' ...
- input type file onchange上传文件的过程中,遇到同一个文件二次上传无效的问题。
不要采用删除当前input[type=file]这个节点,然后再重新创建dom这种方案,这样是不合理的.解释如下:input[type=file]使用的是onchange去做,onchange监听的为 ...
- SSH Secure File Transfer上传文件错误:encountered 1 errors during the transfer解决办法
在使用SSH 工具向Linux服务器上传文件时,弹出 encountered 1 errors during the transfer 错误. 解决方案: 1.准备上传的那个文件所在目录路径存在(), ...
- ie8及其以下版本兼容性问题之input file隐藏上传文件
文件上传时,默认的file标签很难看,而且每个浏览器下都有很大差距.因此我们基本都把真正的file标签给隐藏,然后创建一个标签来替代它.但是由于IE出于安全方面的考虑上传文件时必须点击file的浏览按 ...
- input[type='file']获取上传文件路径案例
最近在项目时,需要获取用户的上传文件的路径,便写了一个demo: <body> <input type="file" name="" valu ...
- input type file onchange上传文件的过程中,同一个文件二次上传无效的问题。
不要采用删除当前input[type=file]这个节点,然后再重新创建dom这种方案,这样是不合理的.解释如下:input[type=file]使用的是onchange去做,onchange监听的为 ...
- input type='file'限制上传文件类型
前端与后台数据进行对接时,就避免不了要使用ajax进行http请求,常用的请求就两个post与get:然而常见的post请求的需求是文件上传,可能我一说到文件上传大家都觉得so easy啊,没什么嘛 ...
随机推荐
- hdu2068-RPG的错排-(dp递推式)
去年看错排公式,死都看不懂,基础扎实之后再来看就略懂了. 公式: dp[ n ] = ( n-1 ) * ( dp[n-1] + dp[n-2] ) 解析公式:比如有n个元素,各对应n个正确位置,dp ...
- foreach中的collection
foreach中collection的三种用法 https://www.cnblogs.com/xiemingjun/p/9800999.html foreach的主要用在构建in条件中,它可以在SQ ...
- 验证码破解 | Selenium模拟登陆微博
模拟登陆微博相对来说,并不难.验证码是常规的5个随机数字字母的组合,识别起来也比较容易.主要是用到许多Selenium中的知识,如定位标签.输入信息.点击等.如对Selenium的使用并不熟悉,请先移 ...
- Vuejs基本使用
一.简单使用 ①首先需要实例化vue:new 出来,注意Vue大小写 ②通过el绑定元素:el 选项的作用就是告诉 Vue 管理模板的入口节点(不要绑定body和html) ③data:是响应式数据, ...
- 封装好的observer.js,用于非父子组件传值,直接调用$on和$emit方法
const eventList = {} const $on = (eventName,callback)=>{ if(!eventList[eventName]){ eventList[eve ...
- 洛谷 P2815 IPv6地址压缩 题解
P2815 IPv6地址压缩 题目背景 (友情提示:IPv6基础知识曾多次出现在NOIP初赛中)Internet Protocol,互联网协议,即为我们常说的IP.我们目前常说的IP主要指它的第四版, ...
- js使浏览器窗口最大化(适用于IE的方法)
这里使用的方法是IE的私有特性,只能在IE中有效.主要是window.moveTo和 window.resizeTo方法. 效果和点击最大化按钮差不多,有一点区别.点击最大化按钮后,浏览器 ...
- sensor【学习笔记】
高通sensor相关介绍不错的文章:https://www.cnblogs.com/linhaostudy/archive/2018/03/16/8578414.html 高通sensor的校准流程: ...
- yapi内网部署 centos
1.部署方案 官方说明: https://hellosean1025.github.io/yapi/devops/index.html 2.需要注意的点 (1)在centos等服务启上最好使用“命令行 ...
- 解决 No IDEA annotations attached to the JDK 1.8和xml文件没有代码提示
Android studio3.3 用着用着突然xml里没有代码联想了,忙着做其他的就没管,写xml的时候就硬写... 然后今天用着突然在class文件上方提示No IDEA annotations ...