简体   繁体   English

使用jQuery Form插件上传Ajax文件-无效的文件错误

[英]Ajax File upload using jQuery Form Plugin - invalid file error

I'm trying to use the jQuery Form Plugin to perform an ajax file upload. 我正在尝试使用jQuery表单插件来执行ajax文件上传。 I copied one of there examples and when I try the upload I get the following text appear: 我复制了其中一个示例,当尝试上传时,出现以下文本:

100% 100%
Invalid file 无效的文件

The console indicates: 控制台指示:

XHR finished loading: XHR完成加载:

but nothing is uploaded. 但没有上传任何内容。

I'm using XAMPP and have enabled permitions on my upload folder using chmod -R 777. The file I'm trying to upload is a .png. 我正在使用XAMPP,并已使用chmod -R 777在我的上载文件夹中启用了许可。我要上载的文件是.png。 Below is the code: 下面是代码:

index.html index.html

<form action="upload.php" method="post" enctype="multipart/form-data">
   <input type="file" name="myfile"><br>
   <input type="submit" value="Upload File to Server">
</form>
<div class="progress">
   <div class="bar"></div >
   <div class="percent">0%</div >
</div>
<div id="status"></div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script src="ajax.js"></script>

ajax.js ajax.js

$(document).ready(function() {

var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');

$('form').ajaxForm({
    beforeSend: function() {
        status.empty();
        var percentVal = '0%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    uploadProgress: function(event, position, total, percentComplete) {
        var percentVal = percentComplete + '%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    success: function() {
        var percentVal = '100%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    complete: function(xhr) {
        status.html(xhr.responseText);
    }
}); 
});

upload.php upload.php

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

Note I think the problem is occurring from the extension not the upload.php as I use this php for other uploads and it works fine. 注意,我认为问题出在扩展而不是upload.php,因为我将此php用于其他上传,并且工作正常。

Thanks for helping. 感谢您的帮助。

If the extension is fine, it could be a size problem: the script is checking $_FILES["file"]["size"] < 20000 . 如果扩展名合适,则可能是大小问题:脚本正在检查$_FILES["file"]["size"] < 20000

I would try to tune that parameter depending on your needs. 我会尝试根据您的需要调整该参数。 The size unit is kilobytes, therefore 20000KB ~ 20MB. 大小单位为千字节,因此为20000KB〜20MB。 Try checking if your image is bigger. 尝试检查图像是否更大。

Also $extension = end(explode(".", $_FILES["file"]["name"])) could be a problem, depending on the file you are uploading: it's supposed to have an extension. $extension = end(explode(".", $_FILES["file"]["name"]))可能是一个问题,具体取决于您要上传的文件:应该具有扩展名。

As a last try, check the value of $_FILES["file"]["type"] , maybe you have some mime-type error. 作为最后的尝试,检查$_FILES["file"]["type"] ,也许您有一些mime-type错误。

If it doesn't bother you to change the plugin then you must try some better plugin like this one i'm using it and its really too developer friendly and user friendly too. 如果它不打扰你改变插件,那么你必须尝试像一些更好的插件, 这个我使用它,它真的太适合开发者和用户友好了。

Grab the full source code with demo files from here . 此处获取带有演示文件的完整源代码。 i'm sure you will not have to make many changes to integrate this plugin to your existing code :) hope this helps you 我敢肯定,您无需进行很多更改即可将该插件集成到您的现有代码中:)希望这对您有所帮助

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

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