简体   繁体   English

无法使用 AJAX 上传大于 50kb 的文件

[英]Can't upload files larger than 50kb with AJAX

I'm not sure why the my code below doesn't handle any files that larger than 50kb on my hosting though I works fine on localhost.尽管我在本地主机上工作正常,但我不确定为什么我下面的代码不能处理任何大于 50kb 的文件。

I tested many different file sizes and I'm pretty sure 50kb is its limit.我测试了许多不同的文件大小,我很确定 50kb 是它的限制。 If a file is larger than 50kb, it is never passed to process.php.如果文件大于 50kb,则永远不会将其传递给 process.php。 If a file is smaller than 50kb, it would be passed to process.php ok.如果文件小于 50kb,它将被传递到 process.php ok。

Is there anyone can help me to fix this.有没有人可以帮我解决这个问题。 I have been stuck in this problem for hours.我已经被这个问题困住了几个小时。

I did set upload_max_filesize in php.ini to 5M.我确实将 php.ini 中的upload_max_filesize设置为 5M。

$( document ).ready(function() {
    $('#img_uploader').on('change', function()
    {
       uploadFiles(this.files);
    }
});

function uploadFiles(fileList)
{
    var xhr = new XMLHttpRequest();
    var formData = new FormData();

    for (var i = 0; i < fileList.length; i++) {
        var file = fileList[i];
        if (!file.type.match('image.*')) {
            continue;
        }
        formData.append('photos[]', file);
        formData.append('request', "uploadImg");
    }

    xhr.open('POST', 'process.php', true);
    xhr.onload = function () {
        if (xhr.status === 200) {
            var data = xhr.responseText;
            console.log(data);
            //convert_json_append_HTML(data);
        } else {
            alert('An error occurred!');
        }
    };
    xhr.send(formData);
}

Updated: Test results更新:测试结果

I had spent 6 hours just to locate the problem.我花了 6 个小时才找到问题所在。

This is really weired.这真的很奇怪。

1/ 4 hours to review all Javascript and PHP code, logged every step to make sure nothing was wrong with the code. 1/ 4 小时来检查所有 Javascript 和 PHP 代码,记录每一步以确保代码没有问题。

  • Tested on localhost with all scenarios.在 localhost 上测试了所有场景。 It worked perfectly.它工作得很好。

2/ Changed these three varables didn't fix the problem regardless what limit I set. 2/ 无论我设置了什么限制,更改这三个变量都没有解决问题。 So I changed them to default.所以我将它们更改为默认值。

  • upload_max_filesize上传最大文件大小
  • memory_limit内存限制
  • post_max_size post_max_size

3/ Browser test: 3/浏览器测试:

Created 2 files: test_1.php and test_2.php.创建了 2 个文件:test_1.php 和 test_2.php。 (basic HTML, no Javascript involved) (基本 HTML,不涉及 Javascript)

test_1.php测试_1.php

<form action="test2.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

test_2.php test_2.php

<?php
var_dump($_FILES);

HTTP : HTTP :

Chrome:铬合金:

  • Files < 50kb: passed文件 < 50kb:通过
  • Files > 50kb: passed文件 > 50kb:通过

Firefox:火狐:

  • Files < 50kb: passed文件 < 50kb:通过
  • Files > 50kb: passed文件 > 50kb:通过

Internet Explorer: IE浏览器:

  • Files < 50kb: passed文件 < 50kb:通过
  • Files > 50kb: passed文件 > 50kb:通过

HTTPS HTTPS

Chrome:铬合金:

  • Files < 50kb: passed文件 < 50kb:通过
  • Files > 50kb: failed文件 > 50kb:失败

Firefox:火狐:

  • Files < 50kb: passed文件 < 50kb:通过
  • Files > 50kb: passed文件 > 50kb:通过

Internet Explorer: IE浏览器:

  • Files < 50kb: passed文件 < 50kb:通过
  • Files > 50kb: passed文件 > 50kb:通过

I'm not sure why the file larger than 50kb can not be passed from test_1.php to test_2.php on HTTPS protocol with Chrome.我不知道为什么大于 50kb 的文件不能通过 Chrome 的 HTTPS 协议从 test_1.php 传递到 test_2.php。 Is there anyone here know the reason?这里有人知道原因吗? Or can try to test it on your own server.或者可以尝试在您自己的服务器上进行测试。

You need to set desired values for 3 variables Check this tutotial您需要为 3 个变量设置所需的值检查此 tutotial

  • upload_max_filesize上传最大文件大小
  • memory_limit内存限制
  • post_max_size post_max_size

I figured out the issue.我想通了这个问题。

Kaspersky Internet Security automatically injects a script into any webpages loaded with Chrome (IE, and FF are not affected).卡巴斯基安全软件会自动将脚本注入任何加载了 Chrome 的网页(IE 和 FF 不受影响)。

The script blocks any package larger than 50kb sent to web server on HTTPS protocol.该脚本会阻止通过 HTTPS 协议发送到 Web 服务器的任何大于 50kb 的包。

在此处输入图片说明

  • Solutions: Kaspersky Internet Security > Settings > Additional > Network >解决方案:卡巴斯基安全软件 > 设置 > 附加 > 网络 >

    • Uncheck "Inject script into web traffic to interact with web pages"取消选中“将脚本注入网络流量以与网页交互”

在此处输入图片说明

我也喜欢这个问题并通过更改 php.ini 中的post_max_size来修复,在我的情况下,默认值是 8 MB 增加到 20 MB,它的工作!

在我的情况下, php.ini中的upload_max_filesizememory_limitpost_max_size被设置为低于 20M 的最小值,这可能导致 ajax 不将请求发送到服务器的 PHP 文件。

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

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