原生xhr ajax上传二进制文件到php保存文件笔记

原生xhr ajax上传二进制文件到php保存文件笔记

800_auto

传统的Content-Type: multipart/form-data; 来上传代码

Content-Type: multipart/form-data; boundary=----WebKitFormBoundarywAAQmLzVPYvLSMkl

html代码

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>XHR 上传文件示例</title>
</head>

<body>
    <input type="file" id="fileInput">
    <button id="uploadButton">上传</button>

    <script>
        const fileInput = document.getElementById('fileInput');
        const uploadButton = document.getElementById('uploadButton');
    
        uploadButton.addEventListener('click', () => {
          const file = fileInput.files[0];
         
         
    // 创建 FormData 对象
    const formData = new FormData();
    
      formData.append('upload_file', file);

    
    // AJAX 发送
    const xhr = new XMLHttpRequest();
    xhr.open('POST', '/upload.php');
    xhr.send(formData);
    
        });
    </script>
</body>

</html>

点击查看剩余70%

{{collectdata}}

网友评论