I am developing a metro app with VS2012 and Javascript
I want to reset the contents of my file input:
<input type="file" id="uploadCaptureInputFile" class="win-content colors" accept="image/*" />
How should I do that?
I am developing a metro app with VS2012 and Javascript
I want to reset the contents of my file input:
<input type="file" id="uploadCaptureInputFile" class="win-content colors" accept="image/*" />
How should I do that?
Particularly for Angular
document.getElementById('uploadFile').value = "";
Will work, But if you use Angular It will say "Property 'value' does not exist on type 'HTMLElement'.any"
document.getElementById() returns the type HTMLElement which does not contain a value property. So, cast it into HTMLInputElement
(<HTMLInputElement>document.getElementById('uploadFile')).value = "";
EXTRA :-
However, we should not use DOM manipulation (document.xyz()) in angular.
To do that angular has provided @ViewChild, @ViewChildren, etc which are document.querySelector(), document.queryselectorAll() respectively.
Even I haven't read it. Better to follows expert's blogs
Resetting a file upload button is so easy using pure JavaScript!
There are few ways to do it, but the must straightforward way which is working well in many browser is changing the filed value to nothing, that way the upload filed gets reset, also try to use pure javascript rather than any framework, I created the code below, just check it out (ES6):
function resetFile() {
const file = document.querySelector('.file');
file.value = '';
}
<input type="file" class="file" />
<button onclick="resetFile()">Reset file</button>
I use for vuejs
<input
type="file"
ref="fileref"
@change="onChange"/>
this.$refs.fileref.value = ''
I miss an other Solution here (2021):
I use FileList to interact with file input.
Since there is no way to create a FileList object, I use DataTransfer, that brings clean FilleList with it.
I reset it with:
file_input.files=new DataTransfer().files
In my case this perfectly fits, since I interact only via FileList with my encapsulated file input element.
There are many approaches and i will suggest to go with ref attachment to input.
<input
id="image"
type="file"
required
ref={ref => this.fileInput = ref}
multiple
onChange={this.onFileChangeHandler}
/>
to clear value after submit.
this.fileInput.value = "";
With angular you can create a template variable and set the value to null
<input type="file" #fileUploader />
<button (click)="fileUploader.value = null">Reset from UI</button>
Or you can create a method to reset like this
component.ts
@ViewChild('fileUploader') fileUploader:ElementRef;
resetFileUploader() {
this.fileUploader.nativeElement.value = null;
}
template
<input type="file"/>
<button (click)="resetFileUploader()">Reset from Component</button>
<input type="file" id="mfile" name="mfile">
<p>Reset</p>
<script>
$(document).ready(function(){
$("p").click(function(){
$("#mfile").val('');
return false;
});
});
I faced the issue with ng2-file-upload for angular. if you are looking for the solution in angular refer below code
HTML:
<input type="file" name="myfile" #activeFrameinputFile ng2FileSelect [uploader]="frameUploader" (change)="frameUploader.uploadAll()" />
component
import { Component, OnInit, ElementRef, ViewChild } from '@angular/core';
@ViewChild('activeFrameinputFile')InputFrameVariable: ElementRef;
this.frameUploader.onSuccessItem = (item, response, status, headers) => {
`this.`**InputFrameVariable**`.nativeElement.value = '';`
};
This could be done like this
var inputfile= $('#uploadCaptureInputFile')
$('#reset').on('click',function(){
inputfile.replaceWith(inputfile.val('').clone(true));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" id="uploadCaptureInputFile" class="win-content colors" accept="image/*" />
<a href="" id="reset">Reset</a>
function resetImageField() {
var $el = $('#uploadCaptureInputFile');
$el.wrap('<form>').closest('form').get(0).reset();
$el.unwrap();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" id="uploadCaptureInputFile" class="win-content colors" accept="image/*" />
<button onclick="resetImageField()">Reset field</button>