使用jquery post发布PHP文件
Let me know if anyone know what is the issue with this code.
如果有人知道这段代码的问题,请告诉我。
Basically i want to upload a file using jQuery
基本上我想用jQuery上传文件
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(event) {
$('#form1').submit(function(event) {
event.preventDefault();
$.post('post.php',function(data){
$('#result').html(data);
});
});
});
</script>
</head>
<body>
<form id="form1">
<h3>Please input the XML:</h3>
<input id="file" type="file" name="file" /><br/>
<input id="submit" type="submit" value="Upload File"/>
</form>
<div id="result">call back result will appear here</div>
</body>
</html>
and my php 'post.php'
和我的php'post.php'
<?php
echo $file['tmp_name'];
?>
Uploaded File name is not returned back. Issue is i couldn't access the uploaded file.
上传的文件名不会返回。问题是我无法访问上传的文件。
Thanks in advance! Shiv
提前致谢!希夫
6 个解决方案
#1
17
Basically i want to upload a file using jQuery
基本上我想用jQuery上传文件
You cannot upload files using AJAX. You could use the jquery.form plugin which uses a hidden iframe:
您无法使用AJAX上传文件。您可以使用jquery.form插件,该插件使用隐藏的iframe:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function(event) {
$('#form1').ajaxForm(function(data) {
$('#result').html(data);
});
});
</script>
</head>
<body>
<form id="form1" action="post.php" method="post" enctype="multipart/form-data">
<h3>Please input the XML:</h3>
<input id="file" type="file" name="file" /><br/>
<input id="submit" type="submit" value="Upload File"/>
</form>
<div id="result">call back result will appear here</div>
</body>
</html>
Also notice the enctype="multipart/form-data"
on the form.
另请注意表单上的enctype =“multipart / form-data”。
Another possibility is to use the HTML5 File API which allows you to achieve that assuming the client browser supports it.
另一种可能性是使用HTML5文件API,它允许您实现假设客户端浏览器支持它。
更多相关文章
- 使用jquery tablesorter插件,有一个选中项目的下拉框
- 非常好用的JQuery自动补全插件bigautocomplete
- 查询下拉框只显示年份(jquery年份插件)
- Jquery+Aajax 批量上传
- jquery省市二级联动插件
- jquery插件——仿新浪微博限制输入字数的textarea
- jQuery插件实现网页底部自动加载-类似新浪微博
- Bootstrap Multiselect插件使用步骤以及常见参数配置介绍
- jQuery基于Canvas的微信运动折线图动态曲线图插件 - wcMotionCha