简体   繁体   English

PHP AJAX 文件上传

[英]PHP AJAX file upload

i've looked all over stackoverflow and the general inte.net and i can't find a simple AJAX file uploader (Just form submission).我查看了整个 stackoverflow 和 general inte.net,但找不到简单的 AJAX 文件上传器(仅提交表单)。 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:如果我可以将此脚本调整为文件上传,那就太好了:

<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谢谢

Here is a simple ajax file uploader这是一个简单的 ajax 文件上传器

Uploadify上传

And if you don't want flash, you may use this one;如果你不想要 flash,你可以使用这个;

AxUploader斧头上传器

It's not that easy.它不是那么容易。 You can't use ajax directly to upload files.不能直接使用ajax上传文件。 Think about the security issues that would cause?想想会引起的安全问题? What if I put "/%USER%/Desktop/CreditCardsAndPasswords.txt" in your str field?如果我将“/%USER%/Desktop/CreditCardsAndPasswords.txt”放入您的 str 字段会怎样? 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.要么,要么自己写一个来满足你的目的。

The way most 'ajax' uploaders work is they dynamically create Html input file elements, and submit a subset of your form (just the file element) to a php script on your website, which then saves the file to whatever temp directory.大多数“ajax”上传器的工作方式是动态创建 Html 输入文件元素,并将表单的子集(仅文件元素)提交到您网站上的 php 脚本,然后将文件保存到任何临时目录。 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.然后,当您提交表单的 rest 时,您可以“赶上”脚本的 rest 并处理上传文件所需的任何处理。

There are many Many ajax 'uploaders' out there.那里有很多 ajax 的“上传者”。 Just do a search and find one, I can't believe all of the ones out there aren't good enough.只需搜索并找到一个,我不敢相信所有的都不够好。

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.我认为默认情况下是 5mb。 and also check for the other ini variable required to increase the size of post data.并检查增加发布数据大小所需的其他 ini 变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM