PHP file-upload using jquery post(使用 jquery post 上传 PHP 文件)
问题描述
如果有人知道此代码有什么问题,请告诉我.
Let me know if anyone know what is the issue with this code.
基本上我想使用 jQuery 上传文件
Basically i want to upload a file using jQuery
<html>
<head>
<script src="aHR0cDovL2NvZGUuanF1ZXJ5LmNvbS9qcXVlcnktbGF0ZXN0Lmpz"></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>
和我的 php 'post.php'
and my php 'post.php'
<?php
echo $file['tmp_name'];
?>
上传的文件名不会返回.问题是我无法访问上传的文件.
Uploaded File name is not returned back. Issue is i couldn't access the uploaded file.
提前致谢!湿婆
推荐答案
基本上我想使用 jQuery 上传文件
Basically i want to upload a file using jQuery
您不能使用 AJAX 上传文件.您可以使用使用隐藏 iframe 的 jquery.form 插件:
You cannot upload files using AJAX. You could use the jquery.form plugin which uses a hidden iframe:
<html>
<head>
<script src="aHR0cDovL2NvZGUuanF1ZXJ5LmNvbS9qcXVlcnktbGF0ZXN0Lmpz"></script>
<script src="aHR0cDovL21hbHN1cC5naXRodWIuY29tL2pxdWVyeS5mb3JtLmpz"></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>
还要注意表单上的 enctype="multipart/form-data"
.
另一种可能性是使用 HTML5 文件 API,假设客户端浏览器支持它,您可以实现这一点.
Another possibility is to use the HTML5 File API which allows you to achieve that assuming the client browser supports it.
这篇关于使用 jquery post 上传 PHP 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 jquery post 上传 PHP 文件


- SoapClient 设置自定义 HTTP Header 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- Laravel 仓库 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01