XMLhttpRequest Ajax PHP 运行不止一次

xmlhttprequest ajax php runs more than once

本文关键字:不止一次 运行 PHP Ajax XMLhttpRequest      更新时间:2023-09-26

我有xmlhttprequest文件上传。问题是该函数运行不止一次。这是我触发文件上传函数的部分:

$('.edit-pic').click(function() {
     if ($('#btnSubmit').val() == 'Edit') {
       return false;
                                }
      else if ($(this).prop('id') == 'profilePic') {
         uploadFile('profilePic');
                                }
      else if ($(this).prop('id') == 'content') {
         uploadFile('coverPic');
                                }
                            });

这是文件上传函数:

function uploadFile(imageType) {
     $('#the-file').click();
     $('#the-file').change(function() {
          var fileInput = $('#the-file');
          var uri = 'upload.php';
          var formData = new FormData();
          var file = fileInput[0].files[0];
          var xhr = new XMLHttpRequest();
          formData.append("the-file", file);
          xhr.upload.addEventListener('progress', onprogressHandler, false);
          xhr.open('POST', uri, true);
          xhr.setRequestHeader('X-File-Name', file.name + '&' + imageType);
              xhr.onload = function() {
                 if (this.readyState == 4 && this.status == 200) {
                     var response = JSON.parse(this.response);
                          if (response.imageType == 'coverPic') {
                               $('#content').css('background-image', 'url("' + response.tempUrl + '")'); }
                          else if (response.imageType == 'profilePic') {
                               $('#profilePic').prop('src', response.tempUrl);
                                            }
                                        }
                                    };
                 function onprogressHandler(e) {
                    var percent = e.loaded / e.total * 100;
                    console.log('Upload progress: ' + percent + '%');                                        
                                    }
                                    xhr.send(formData);
                                });
                            }
                            ;

这是我的 php 文件中的内容:

if (!file_exists($_FILES["the-file"]["name"])) {
            $fileName = $_SERVER['HTTP_X_FILE_NAME'];
            $fileType = $_FILES['the-file']['type'];
            $fileContent = file_get_contents($_FILES['the-file']['tmp_name']);
            $dataUrl = 'data:' . $fileType . ';base64,' . base64_encode($fileContent);
            $tempUrl = "";
            list($fileName, $imageType) = explode('&', $fileName);
            if ($imageType == 'profilePic') {
                move_uploaded_file($_FILES['the-file']['tmp_name'], "temp/profile/" . $_FILES['the-file']['name']);
                $tempUrl = "temp/profile/" . $_FILES['the-file']['name'];
            } else if ($imageType == 'coverPic') {
                move_uploaded_file($_FILES['the-file']['tmp_name'], "temp/cover/" . $_FILES['the-file']['name']);
                $tempUrl = "temp/cover/" . $_FILES['the-file']['name'];
            }
            $json = json_encode(array(
                'name' => $fileName,
                'type' => $fileType,
                'dataUrl' => $dataUrl,
                'tempUrl' => $tempUrl,
                'imageType' => $imageType
            ));
            echo $json;

我的问题是,每次我上传新图片时,该功能的运行次数与我单击文件上传的次数一样多。如果我单击它 4 次进行测试,它会运行 4 次并将个人资料和封面图片的 img src 更改为我选择的最后一张图片。我尝试返回假,事件。防止违约等在这一点上,我愿意接受任何建议。对不起格式,这里有很多代码。

您是否尝试过将事件作为参数传递给 click()?

$('.edit-pic').click(function( e ) {
    e.preventDefault();
     if ($('#btnSubmit').val() == 'Edit') {
       return false;
                                }
      else if ($(this).prop('id') == 'profilePic') {
         uploadFile('profilePic');
                                }
      else if ($(this).prop('id') == 'content') {
         uploadFile('coverPic');
                                }
                            });