使用AJAX提交HTML表单类型的文件 [英] Submit HTML form type file with AJAX

查看:132
本文介绍了使用AJAX提交HTML表单类型的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格和一个div。我希望用户选择一个文本文件,并点击提交,并且将填充DIV与文件的内容。我想做到这一点,而无需刷新页面。

 <形式的行动=action.php的方法=邮报是enctype =的multipart / form-data的>
点击选择文件按钮,浏览要导入的文件< BR />< BR />
<输入类型=文件名称=filelocation值=浏览/>< BR />
<输入类型=提交名称=submitformVALUE =提交/>
< /形式GT;

< D​​IV ID =filecontent>

< / DIV>
 

解决方案

即使世界没有简单的方法来做到这一点,但你可以使用iframe用Ajax调用来获取你的文件到服务器,并用它正常的AJAX工作方法

 <形式的行动=upload.php的方法=邮报是enctype =的multipart / form-data的目标=upload_target的onsubmit =startUpload(); >
文件:其中,输入名称=MYFILE类型=文件/>
<输入类型=提交名称=submitBtn值=上传/>
< /形式GT;
 

目标= yourhiddeniframe

下面是一个完整的HOWTO: http://www.ajaxf1.com/tutorial/ajax-file-upload -tutorial.html

问候

I have a form and a div. I want the user to select an text file and hit submit and that will populate the div with the content of the file. I would like to do that without having to refresh the page.

<form action="action.php" method="post" enctype="multipart/form-data">
Click the "Choose File" button and browse for the file to import.<br /><br />
<input type="file" name="filelocation" value="Browse" /><br />
<input type="submit" name="submitform" value="Submit" />
</form>

<div id="filecontent">

</div>

解决方案

Theres is no easy way to do that, but u can use an Iframe with an ajax call to get your file to the server and work with it the normal ajax way

<form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" >
File: <input name="myfile" type="file" />
<input type="submit" name="submitBtn" value="Upload" />
</form>

look at target="yourhiddeniframe"

here is a complete howto: http://www.ajaxf1.com/tutorial/ajax-file-upload-tutorial.html

greetings

这篇关于使用AJAX提交HTML表单类型的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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