[英]upload file using ajax,jquery,php
I want to upload a file and send to server without refreshing page. 我想上传文件并发送到服务器而不刷新页面。
I have following line in my HTML file 我的HTML文件中有以下一行
<form id="FileUploader" enctype="multipart/form-data" >
<input type="file" name="mfile" id="mfile" style='width:100%;' onchange="uploaded()">
</form>
function uploaded()
{
alert($('form#FileUploader')[0]);
var formData=new FormData($('form#FileUploader')[0]);
//alert(formData);
$.ajax({
url: "<?php echo $_SESSION['webpage']."/upload" ?>",
type: "POST",
async: true,
dataType: "JSONP",
data : formData
})
.success (function(response){
alert(response);
})
.error (function() { alert("Error") ; }) ;
}
upload.php file upload.php文件
if ($_FILES["mfile"]["error"] >0 )
{
echo "Error: " ;
}
else
{
if (file_exists("upload_email_files/" . $_FILES["mfile"]["name"]))
{
echo $_POST["file"]. " already exists. ";
}
else
{
$otp= move_uploaded_file('$_FILES["mfile"]"name"]','/../upload_templates/');
}
}
It's not working .Can anybody help me on this ? 它没有用。有人可以帮我吗? It is not coming in upload.php and giving me error Illegal Invocation.
它没有出现在upload.php中,并给我错误非法调用。
Thanks, Shirish 谢谢,Shirish
in the case of " without refreshing page ", you can use hidden iframe. 在“ 不刷新页面 ”的情况下,您可以使用隐藏的iframe。
For reference, 以供参考,
http://viralpatel.net/blogs/ajax-style-file-uploading-using-hidden-iframe/ , http://viralpatel.net/blogs/ajax-style-file-uploading-using-hidden-iframe/ ,
http://joekuan.wordpress.com/2009/06/12/ajax-a-simplified-version-of-file-upload-form-using-iframe/ http://joekuan.wordpress.com/2009/06/12/ajax-a-simplified-version-of-file-upload-form-using-iframe/
Check this out I haven't tested it hope this will work http://blog.new-bamboo.co.uk/2012/01/10/ridiculously-simple-ajax-uploads-with-formdata 检查一下,我还没有测试过,希望它能正常工作http://blog.new-bamboo.co.uk/2012/01/10/ridiculously-simple-ajax-uploads-with-formdata
<form id="FileUploader" enctype="multipart/form-data" >
<input type="file" name="mfile" id="mfile" style='width:100%;' onchange="uploaded()">
</form>
function uploaded()
{
alert($('form#FileUploader')[0]);
var formData=new FormData($('form#FileUploader'));//remove [0]
//alert(formData);
$.ajax({
url: "<?php echo $_SESSION['webpage']."/upload" ?>",
type: "POST",
//async: true,//Remove this line
//dataType: "JSONP",//Remove this line
data : formData
})
.success (function(response){
alert(response);
})
.error (function() { alert("Error") ; }) ;
}
Try this one :) 试试这个:)
<form id="FileUploader" enctype="multipart/form-data" >
<input type="file" name="mfile" id="mfile" style='width:100%;' onchange="uploaded()">
</form>
$(document).ready(function() {
$("#form-geninfo").submit(function(e)
{
e.preventDefault();
alert($('form#FileUploader')[0]);
var formData=new FormData($('form#FileUploader')[0]);
//alert(formData);
$.ajax({
url: "<?php echo $_SESSION['webpage']."/upload" ?>",
type: "POST",
async: true,
dataType: "JSONP",
data : formData
})
.success (function(response){
alert(response);
})
.error (function() { alert("Error") ; }) ;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.