You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XMLHttpRequest无法上传部分PDF文件,如何排查问题?

问题:部分小体积PDF文件上传失败排查

我需要将PDF文件上传至服务器,流程是通过文件输入框把文件传入JS函数,再用XMLHttpRequest发送。目前多数PDF(包括32MB的)都能正常上传,但部分更小的PDF(比如4MB)却无法上传。

用于排查错误的JS函数

function uploadFiles(input_id) {
    const input = document.getElementById(input_id);
    const files = input.files;
    if (files.length == 0) {
        alert("No Files selected!");
        return;
    }

    let formData = new FormData();
    for (let i=0; i<files.length; i++) {
        formData.append(i, files[i]);
    }
    console.log(formData);
    
    return new Promise( resolve => {
        request = new XMLHttpRequest();
        request.addEventListener("error", handleEvent);
        request.addEventListener("loadstart", handleEvent);
        request.addEventListener("load", handleEvent);
        request.addEventListener("loadend", handleEvent);
        request.addEventListener("progress", handleEvent);
        request.addEventListener("error", handleEvent);
        request.addEventListener("abort", handleEvent);
        request.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                resolve(this.responseText);
            } else {
                return this.responseText;
            }
        }
        request.open("POST", "includes/fileUpload.inc.php");
        request.send(formData);
    } );
}

function handleEvent(e) {
    console.log(e);
}

测试用目标PHP文件

<?php
echo "hi";
return;
?>

上传现象

  • 正常上传:文件几秒内完成上传,最终返回"hi"
  • 失败场景:触发loadstart事件后,约60秒无响应,随后触发progress、error和loadend事件,但控制台没有有效错误信息,也无响应内容
  • 跨浏览器测试:Chrome和Firefox中,相同的问题PDF都会上传失败
  • 特殊测试:将问题PDF打包成.rar后可正常上传,说明问题是PDF文件本身特性导致

已尝试的替代方案

用jQuery的.ajax()替换XMLHttpRequest的Promise实现,但问题PDF仍上传失败,代码如下:

$.ajax({
    method: "POST",
    url: "includes/fileUpload.inc.php",
    data: formData,
    processData: false,
    contentType: false,
    success: function(response) {
        console.log(response);
    }
});

当前PHP配置(已排除常见限制问题)

  • memory_limit: 256M
  • max_execution_time: 600
  • max_input_time: 60
  • post_max_size: 96M
  • upload_max_filesize: 96M

请问我该如何获取更多信息以追溯错误根源?

内容的提问来源于stack exchange,提问作者Bonsai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 01:58:08