Javascript PHP AJAX 文件上传
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10091154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP AJAX file upload
提问by blasteralfred Ψ
i've looked all over stackoverflow and the general internet and i can't find a simple AJAX file uploader (Just form submission). The ones i have found aren't flexible enough and don't fit my purpose. If i could ajust this script to file uploads, that would be great:
我已经查看了 stackoverflow 和一般互联网,但找不到一个简单的 AJAX 文件上传器(只需提交表单)。我发现的那些不够灵活,不符合我的目的。如果我可以将此脚本调整为文件上传,那就太好了:
<script type="text/javascript">
function upload(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("hint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax/link.php?q="+str,true);
xmlhttp.send();
}
In the HTML i would have
在 HTML 我会有
<input type='file' onblur='upload(this.value)'>
Thanks
谢谢
回答by blasteralfred Ψ
Here is a simple ajax file uploader
这是一个简单的ajax文件上传器
And if you don't want flash, you may use this one;
如果你不想要 flash,你可以使用这个;
回答by Authman Apatira
It's not that easy. You can't use ajax directly to upload files. Think about the security issues that would cause? What if I put "/%USER%/Desktop/CreditCardsAndPasswords.txt" in your str field? In short, you need to find and use those 'non flexible' uploaders you were talking about and will have to edit them to your needs. Either that, or write one yourself to fit your purpose.
那并没那么简单。不能直接使用ajax上传文件。想想会导致的安全问题?如果我在 str 字段中输入“/%USER%/Desktop/CreditCardsAndPasswords.txt”会怎样?简而言之,您需要找到并使用您所谈论的那些“非灵活”上传器,并且必须根据您的需要对其进行编辑。要么,要么自己写一个来满足你的目的。
The way most 'ajax' uploaders work is they dynamically create Html input file elements, and submit a subsetof your form (just the file element) to a php script on your website, which then saves the file to whatever temp directory. Then when you submit the rest of the form, you can 'catch up' with the rest of the script and handle whatever processing is necessary with the uploaded files.
大多数“ajax”上传器的工作方式是动态创建 Html 输入文件元素,并将表单的子集(仅文件元素)提交到您网站上的 php 脚本,然后将文件保存到任何临时目录。然后,当您提交表单的其余部分时,您可以“赶上”脚本的其余部分并处理上传的文件所需的任何处理。
There are many Many ajax 'uploaders' out there. Just do a search and find one, I can't believe all of the ones out there aren't good enough.
那里有很多很多 ajax '上传者'。只需搜索并找到一个,我无法相信所有这些都不够好。
回答by Dilraj Singh
var data = new FormData();
data.append("image_upload",jQuery("#file_upload_input")[0].files[0]);
jQuery.ajax({
url: 'url',
type: 'POST',
data: data,
cache: false,
processData: false, // Don't process the files
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
success: function(data, textStatus, jqXHR)
{
console.log(data, textStatus, jqXHR);
},
error: function(jqXHR, textStatus, errorThrown)
{
console.log(jqXHR, textStatus, errorThrown);
}
});
Please also check for the post_max_size ini variable, you will get it using echo phpinfo();
还请检查 post_max_size ini 变量,您将使用 echo phpinfo(); 获得它。
you have to set max file size you are going to upload. By default it is 5mb I think. and also check for the other ini variable required to increase the size of post data.
您必须设置要上传的最大文件大小。我认为默认情况下它是 5mb。并检查增加发布数据大小所需的其他 ini 变量。