core/client/src/jsMain/kotlin/dev/inmo/postssystem/client/utils/DownloadFile.kt

20 lines
697 B
Kotlin
Raw Normal View History

2021-11-24 07:52:27 +00:00
package dev.inmo.postssystem.client.utils
import dev.inmo.postssystem.features.files.common.FullFileInfo
import dev.inmo.micro_utils.common.toArrayBuffer
2022-01-22 16:59:41 +00:00
import io.ktor.utils.io.core.readBytes
2021-11-24 07:52:27 +00:00
import kotlinx.browser.document
import org.w3c.dom.HTMLAnchorElement
import org.w3c.dom.url.URL
import org.w3c.files.Blob
fun triggerDownloadFile(fullFileInfo: FullFileInfo) {
val hiddenElement = document.createElement("a") as HTMLAnchorElement
2022-01-22 16:59:41 +00:00
val url = URL.createObjectURL(Blob(arrayOf(fullFileInfo.inputProvider().readBytes().toArrayBuffer())))
2021-11-24 07:52:27 +00:00
hiddenElement.href = url
hiddenElement.target = "_blank"
hiddenElement.download = fullFileInfo.name.name
hiddenElement.click()
}