上载文件和ajax/jquery失败 [英] Upload file and ajax/jquery fails
本文介绍了上载文件和ajax/jquery失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正试图在联系人表单中添加文件上传功能,但是由于某些原因它无法正常工作.上载脚本本身可以正常工作,但是当我将其添加到联系代码时,jQuery脚本会使其失败.
I'm trying to add a file upload function to a contact form, but it won't work for some reason. The upload script works fine by itself, but when I add it to the contact code, the jquery script makes it fail.
您可以说,我绝不是专家.
As you can tell, I am by no means an expert.
pontact.php
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$('#contact_form').submit(function(e){
e.preventDefault();
var form = $(this);
var post_url = form.attr('action');
var post_data = form.serialize();
$('#loader', form).html('<img src="loader.gif" /> Please Wait...');
$.ajax({
type: 'POST',
url: post_url,
data: post_data,
success: function(msg) {
$(form).fadeOut(500, function(){
form.html(msg).fadeIn();
});
}
});
});
});
</script>
</head>
<body>
<form action="process.php" method="post" id="contact_form">
<div>
<label for="name">Your Name:</label>
<input type="text" name="name" id="name" value="" tabindex="1" />
</div>
<div>
<label for="email">Your Email:</label>
<input type="text" name="email" id="email" value="" tabindex="2" />
</div>
<div>
<label for="message">Message:</label>
<textarea cols="40" rows="8" name="message" id="message"></textarea>
</div>
<div>
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Send this file: <input name="userfile" type="file" />
</div>
<div id="loader">
<input type="submit" value="Submit" />
</div>
</form>
</body>
</html>
Process.php
$uploaddir = '/var/www/download/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
推荐答案
By default you cant upload a file using ajax,( Why can't I upload files asynchronously? )
但是有一些技术,包括您可以使用的iframe或Flash插件
but there are a few techinques, including iframes or flash plugins you could use
什么是最好的多重文件JavaScript/Flash文件上传器?
希望这会有所帮助
这篇关于上载文件和ajax/jquery失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文