37 lines
1.0 KiB
GDScript
37 lines
1.0 KiB
GDScript
extends Node
|
|
|
|
signal uploaded_file(filename, file_type, data)
|
|
|
|
var js_callback: JavaScriptObject
|
|
var js_interface: JavaScriptObject
|
|
|
|
func _ready() -> void:
|
|
if OS.get_name() != 'HTML5' or !OS.has_feature('JavaScript'):
|
|
return
|
|
|
|
var file := File.new()
|
|
match file.open('res://data/html5_file_picker.js', File.READ):
|
|
OK:
|
|
JavaScript.eval(file.get_as_text(), true)
|
|
var error:
|
|
print_stack()
|
|
return
|
|
|
|
js_callback = JavaScript.create_callback(self, '_upload_handler')
|
|
js_interface = JavaScript.get_interface('_FilePicker')
|
|
|
|
func _upload_handler(_args):
|
|
var filename = _args[0]
|
|
var file_type = _args[1]
|
|
# Workaround for JavaScriptObject being seemingly impossible to unwrap (no docs)
|
|
var data_key: String = '_FilePickerData["%s"]' % filename
|
|
var data = JavaScript.eval(data_key, true)
|
|
JavaScript.eval('delete '+data_key, true)
|
|
print(_args)
|
|
emit_signal('uploaded_file', filename, file_type, data)
|
|
|
|
func upload_file() -> void:
|
|
if OS.get_name() != 'HTML5' or !OS.has_feature('JavaScript'):
|
|
return
|
|
js_interface.upload(js_callback)
|