Ad
How To Reuse Argument From A Function?
I have a function that shows image after input(type=file) loads. I need to save the source attribute of image, so I could use it furthermore. This is the code:
function readURL(input, callback) {
if (input.files && input.files[0]) {
let reader = new FileReader();
reader.onload = function (e) {
regForm.img.setAttribute('src', e.target.result); // I need to save this somehow, to use it later in a POST Request body.
}
reader.readAsDataURL(input.files[0]);
}
}
Ad
Answer
You can use promise like this:
function readURL(input) {
return new Promise((resolve) => {
if (input.files && input.files[0]) {
let reader = new FileReader()
reader.onload = function (e) {
regForm.img.setAttribute('src', e.target.result)
reolve(e.target.result)
}
reader.readAsDataURL(input.files[0])
}
})
}
Whenever you want the source const src = await readURL(YOUR FILE)
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad