0

xmlhttprequest ファイルをアップロードしました。問題は、関数が複数回実行されることです。これは、fileUpload 関数を起動する部分です。

$('.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');
                                }
                            });

fileUpload 関数は次のとおりです。

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 が、最後に選択した写真に変更されます。false、event.PreventDefaultなどを返そうとしました。この時点で、どんな提案も受け付けています。形式については申し訳ありませんが、ここには多くのコードがあります。

4

1 に答える 1

0

イベントを引数として 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');
                                }
                            });
于 2013-11-13T18:49:20.337 に答える