問題描述
如果有人知道此代碼有什么問題,請告訴我.
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="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>
和我的 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="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>
還要注意表單上的 enctype="multipart/form-data"
.
另一種可能性是使用 HTML5 文件 API,假設(shè)客戶端瀏覽器支持它,您可以實現(xiàn)這一點.
Another possibility is to use the HTML5 File API which allows you to achieve that assuming the client browser supports it.
這篇關(guān)于使用 jquery post 上傳 PHP 文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!