我正在通過構建一個圖像共享Web應用程序來學習Scalajs。 在一個表單中,我有一個經典的文件輸入標籤,並希望通過HTTP Post請求將它使用Ajax和jQuery上傳到遠程服務器。如何使用Scalajs中的Ajax上傳文件
這是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>
這裏是底層的Scala代碼:
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)
}
}
}
}
})
任何幫助,將不勝感激