I'm learning Scalajs by building an image sharing webapp. In a form, I have a classic file input tag and would like to upload it using Ajax and jQuery to a remote server through a HTTP Post request.
This is the html:
<input id="postTitle" class="form-control" placeholder="Title" type="text" aria-describedby="basic-addon1">
<input id="file" class="form-control" placeholder="Browse" type="file" aria-describedby="basic-addon1">
<input id="tags" class="form-control" placeholder="Tags in format ['tag 1', 'tag 2',...]" type="text">
<button id="postButton" class="btn btn-success">Submit</button>
And here is the underlying Scala code:
lazy val submitElement = jQuery("#postButton")
jQuery(() => {
submitElement.click {
(_: JQueryEvent) => {
dom.ext.Ajax.post(
url = [server_url],
data = ???, // <-- How do I get the file?
headers = Map("Content-Type" -> "application/json")
).foreach { xhr =>
if (xhr.status == 200) {
val x = JSON.parse(xhr.responseText)
println(x)
}
}
}
}
})
Any help would be greatly appreciated