JavaScript:上传文件 [英] JavaScript: Upload file

查看:108
本文介绍了JavaScript:上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < 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:


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:

这篇关于JavaScript:上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆