[英]How can i Redirect Chrome/Firefox Extension with same opened tab if we click on same URL again?
[英]How can i select same file with chrome like firefox in Angular 4
我有一个编辑器,即使它们相同(重复),我也可以在其中添加多个文件。
就我而言,当我从Firefox中选择一个文件时,它将被添加到编辑器中。 第二次,当我重新选择同一文件时,它将再次正确添加到编辑器中。 (工作不错)
但
当我第一次使用谷歌浏览器时,它可以正常工作,但是第二次,它没有使用相同的文件,因为先前已经选择了相同的文件,所以它没有使用(当我取消并添加相同的文件时,它被添加了)
我的HTML是这样的:
<input type="file"name="addFiles" multiple>
我看过一篇文章说,当我使用多个时,它会重置,但不是。
无论如何,通过使用以下jquery代码,我得到了我想要的(在编辑器中将文件设置为空后添加文件!
jQuery('input[type=file]').val('');
我正在使用Angular 4我不想使用jquery,我该怎么做。
在Angular 4中上传文件
在my-fileupload.component.ts文件中
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { FormGroup, FormControl, FormBuilder} from '@angular/forms';
import { SendToService } './_service/service';
@Component({
selector: 'app-my-fileupload',
templateUrl: './app-my-fileupload.component.html',
styleUrls: ['./app-my-fileupload.component.scss'],
providers: [
SendToService
]
})
export class MyFileuploadComponent implements OnInit {
@ViewChild('fileInput') fileInput: ElementRef;
public MyForm : FormGroup;
public Files = new FormControl();
constructor(private fb: FormBuilder, private sendToService:
SendToService){}
ngOnInit() {
this.MyForm = this.fb.group({
Files: this.Files,
});
}
onFileChange(event) {
if (event.target.files.length > 0) {
this.filesToUpload = <Array<File>>event.target.files; // FILE
let size = 5 * 1024 * 1024; // 5MB
for (let i = 0; i < this.filesToUpload.length; i++) {
if (this.filesToUpload.size > 5000000) {
// Message for file size
} else {
this.MyForm.get('Files').setValue(this.filesToUpload);
}
}
}
}
onSubmit(){
const formObj = this.UploadFile.getRawValue();
this.sendToService.UploadFile(formObj).subscribe(response => {
console.log(response) // response = success
if (response == success) {
this.MyForm.reset();
}
}
}
}
在send-to-service.service.ts文件中
import { Injectable, OnInit } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
@Injectable()
export class SendToService {
Url_app = 'url';
constructor(private _http: Http) { }
UploadFile(formObj: any) {
let formData = new FormData();
for (let i = 0; i < formObj.length; i++) {
formData.append('Files[]', formObj[i], formObj[i]['name']);
}
return this._http.post( Url_app , formData);
}
}
在my-fileupload.component.html文件中
<input id="Files" type="file" #filesInput (change)="onFileChange($event)"
[multiple]="true" />
如果您不想这样做
使用这个方法
@ViewChild('fileInput') fileInput: ElementRef;
this.filesInput.nativeElement.value = ""
我们使用ViewChild和ElementRef来访问输入
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.