JavaScript:上传文件 [英] JavaScript: Upload file
问题描述
< input id =image-filetype =文件/>
这将创建一个按钮,允许网页的用户通过操作系统选择一个文件文件打开...对话框。
假设用户点击该按钮,在对话框中选择一个文件,然后点击确定按钮关闭
所选文件名现在存储在:
document.getElementById(image-file)。value
现在假设服务器处理
如何将文件发送到/ upload / image?
另外,如何收听文件上传完成的通知?
试图使用ajax上传文件,将表单提交到 / upload / image
。
< form enctype =multipart / form-dataaction =/ upload / imagemethod =post>
< input id =image-filetype =file/>
< / form>
如果您想在后台上传图片(例如不提交整个表格)可以使用ajax:
- stackoverflow.com/questions/2320069/jquery-ajax-file-upload\"> jQuery Ajax文件上传 使用文件上传的Ajax
Let's say I have this element on the page:
<input id="image-file" type="file" />
This will create a button that allows the users of the web page to select a file via an OS "File open..." dialog in the browser.
Let's say the user clicks said button, selects a file in the dialog, then clicks the "Ok" button to close the dialog.
The selected file name is now stored in:
document.getElementById("image-file").value
Now, let's say that the server handles multi-part POSTs at the URL "/upload/image".
How do I send the file to "/upload/image"?
Also, how do I listen for notification that the file is finished uploading?
Unless you're trying to upload the file using ajax, just submit the form to /upload/image
.
<form enctype="multipart/form-data" action="/upload/image" method="post">
<input id="image-file" type="file" />
</form>
If you do want to upload the image in the background (e.g. without submitting the whole form), you can use ajax:
- Asynchronous file upload (AJAX file upload) using jsp and javascript
- jQuery Ajax File Upload
- Ajax using file upload
这篇关于JavaScript:上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!