diff options
author | Robert Jaros <rjaros@finn.pl> | 2018-03-29 01:05:02 +0200 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2018-03-29 01:05:02 +0200 |
commit | 5ff62fa68f1c6d9693aeb4c27116b77c43e1a309 (patch) | |
tree | 98755ab99b1920ebfa22fd0e26229560b5a68f4f /src/main/kotlin/pl/treksoft/kvision/utils | |
parent | 8a1370e0f661d2c90fd5aed8e868341119a866cd (diff) | |
download | kvision-5ff62fa68f1c6d9693aeb4c27116b77c43e1a309.tar.gz kvision-5ff62fa68f1c6d9693aeb4c27116b77c43e1a309.tar.bz2 kvision-5ff62fa68f1c6d9693aeb4c27116b77c43e1a309.zip |
Components for file upload.
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/utils')
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/utils/Utils.kt | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/utils/Utils.kt b/src/main/kotlin/pl/treksoft/kvision/utils/Utils.kt index d638f79e..ba9467c3 100644 --- a/src/main/kotlin/pl/treksoft/kvision/utils/Utils.kt +++ b/src/main/kotlin/pl/treksoft/kvision/utils/Utils.kt @@ -23,6 +23,9 @@ package pl.treksoft.kvision.utils +import kotlinx.coroutines.experimental.suspendCancellableCoroutine +import org.w3c.files.File +import org.w3c.files.FileReader import pl.treksoft.kvision.KVManager import pl.treksoft.kvision.core.CssSize import pl.treksoft.kvision.core.UNIT @@ -96,7 +99,7 @@ val Int.mm: CssSize /** * Extension property to convert Int to CSS in units. */ -@Suppress("FunctionNaming") +@Suppress("TopLevelPropertyNaming") val Int.`in`: CssSize get() { return Pair(this, UNIT.`in`) @@ -204,3 +207,20 @@ fun Date.toStringF(format: String = "YYYY-MM-DD HH:mm:ss"): String { * @return true if the current browser is IE11 */ fun isIE11(): Boolean = window.navigator.userAgent.matches("Trident\\/7\\.") + +/** + * Suspending extension function to get file content. + * @return file content + */ +@Suppress("EXPERIMENTAL_FEATURE_WARNING") +suspend fun File.getContent() = suspendCancellableCoroutine<String> { cont -> + val reader = FileReader() + reader.onload = { + @Suppress("UnsafeCastFromDynamic") + cont.resume(reader.result) + } + reader.onerror = { e -> + cont.resumeWithException(Exception(e.type)) + } + reader.readAsDataURL(this@getContent) +} |