angular - 需要在角度模式确认框中单击“是”时上传所选文件
问题描述
我是 Web 开发新手,在实现上传功能时遇到问题。在这里,选择要上传的文件时会发生一个确认框,然后单击“是”按钮将数据上传到服务器。没有单独的上传按钮
我在选择文件时收到确认框。但不确定如何直接将数据上传到服务器
组件.html:
<div style="text-align: center">
<div class="col-xs-9">
<div class="form group">
<label for="Upload" style="display: block;">Please select file to upload</label>
<input type="File" id="btnUpload" name="Upload" value="Upload" (change)="Onselect($event)" style="padding-left: 450px;" #Upload />
</div>
</div>
</div>
<div *ngIf="filesIsSelected"
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Confirm</h4>
</div>
<div class="modal-body">
<p>Do you want to upload selected file?</p>
<div align= center>
<button type="button" class="btn btn-danger" data dismiss="modal">
<span class="glyphicon glyphicon-remove"></span> No
</button>
<button type="button" class="btn btn-success" data-dismiss="modal" (click)="proceedUpload()">
<span class="glyphicon glyphicon-floppy-disk"></span>Yes
</button>
</div>
</div>
</div>
</div>
</div>
组件.ts:
Onselect(event){
debugger;
if(event.target.files && event.target.files.length > 0) {
this.filesIsSelected = true;
const files = event.target.files;
for (let i = 0; i < files.length; ++i) {
const file = files[i];
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
this.fileToUpload.push({
filenameWithExt: file.name,
filename: file.name.split('.')[0],
filetype: file.type,
});
};
}
}
else
{
this.filesIsSelected = false;
}
}
proceedUpload(){
let formData: FormData = new FormData();
formData.append('Upload Files', fileToUpload);
this.http.post("url", formData ).map(res => res.json())
.catch(error => Observable.throw(error))
.subscribe(
data => console.log('success'),
error => console.log(error)
)
}
}
proceedUpload() 是我需要帮助的部分。单击“是”后,我无法找到访问所选文件并将其直接上传到服务器的方法。
解决方案
我认为您应该在文件输入的“onChange”方法中实现一些功能,以准备要发送的图像:
function prepareMyImg(event){
if (event.target.files && event.target.files.length > 0) {
const files = event.target.files;
for (let i = 0; i < files.length; ++i) {
const file = files[i];
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
this.imagenes.push({
filenameWithExt: file.name,
filename: file.name.split('.')[0],
filetype: file.type,
data: reader.result.split(',')[1]
});
};
}
}
}
然后,当您进行Upload() 时,您只需添加图像对象数组。
formData.append('Files', this.imagenes);
此处提供可能的工作解决方案:https ://stackblitz.com/edit/angular-6gbbje
推荐阅读
- unity3d - 将对象旋转到鼠标方向
- c++ - VS2017 15.8.3版本成功编译内联方法不返回必填值
- javascript - 事件循环、队列等是 JavaScript 运行时或引擎的一部分吗?
- r - ggplot2堆积条,把NA放在上面
- azure - Azure on Premise 数据网关的服务总线组件 - 定价
- python - Python 2.7 自定义异常没有被捕获
- java - 根据特定的单元格值划分excel表
- r - 在 R 中显示密度图(ggplot2)的 x 轴上的显式点
- css - 如何在不使用 React Native 中的字符串的情况下将宽度(100%)设置为与其父级相同?
- c# - WPF 窗口内托管本机 HWND 控件的 DPI 缩放问题