Ajax文件上传'尝试加载资源时出错“和Safari中的网络连接丢失

I m trying to upload a file and a thumb of that file (using the cropper v2.3.0). This code work on all other browser but in safari it gives an error.

The problem describe as follow:

  1. on safari browser on desktop when upload the file then the below error occurred and used other then safari browser there is no error and get success message.

  2. I test both ways that first upload only the cropped image that is in base64 encode or also in blob as file with appending into formData but on both ways that error not resolved.

  3. I also tried to upload the image only then error occurred sometimes or sometimes not.

  4. if use cropper adjust then the error occurred (this is my assumption)

My js Code to submit the form

function addFile() {
 $("#result").html("");
 var myForm = $('#mainForm');
 var formData = new FormData(myForm[0]);

 $.ajax({
    url: "action.php", // Url to which the request is send
    type: "POST", // Type of request to be send, called as method
    data: formData, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
    contentType: false, // The content type used when sending data to the server.
    cache: false, // To unable request pages to be cached
    processData: false, // To send DOMDocument or non processed data file it is set to false
    dataType: "json",
    success: function (data)            // A function to be called if request succeeds
    {
        $("#result").html(data.response + " : " + data.message);
    },
    error: function (res) {
        $("#result").html(res.responseText);
    }
 });
 return false;
}

my action php code

    <?php

    $uploadThumbnailPath    = "dir";
    $thumbImgData           = $_POST['thumbImg'];

    $numberOfImages = 1;

    $isImageUploaded = 0;
    if ($thumbImgData != "") {
        //thumbnail image uploading code
        list($type, $thumbImgData) = explode(';', $thumbImgData);
        list(, $thumbImgData) = explode(',', $thumbImgData);
        $thumbImgData = base64_decode($thumbImgData);

        $myTimeStamp      = "thumbImg_" . time() . uniqid();
        $displayImageName = $myTimeStamp . ".png";
        $dir              = $uploadThumbnailPath;
        if (file_put_contents("$dir/$displayImageName", $thumbImgData)) {
            $jpgFormatImageName = $myTimeStamp . ".jpg";
            convertPNGtoJPG("$dir/$displayImageName", "$dir/$jpgFormatImageName", 238, 238, 238, 1);
            if (file_exists("$dir/$displayImageName")) {
                unlink("$dir/$displayImageName");
            }
            $isImageUploaded = 1;
        }
    } else {
        $arrayResponse = array("response" => "thumbImg_BLANK", "message" => 'thumbImg_BLANK');
        echo json_encode($arrayResponse);
        exit;
    }


    for ($i = 1; $i <= $numberOfImages; $i++) {

        if (isset($_POST["imgName$i"])) {
            $itemImagesName = "";
        } else {
            $itemImagesName = $_FILES["imgName$i"]['name'];
        }
        if ($itemImagesName != "") {
            $extension                   = pathinfo($itemImagesName, PATHINFO_EXTENSION);
            $uploadNewFileNameWithoutExt = "image_" . md5($i . time());
            $uploadDirPath               = "dir/p/";

            $uploadNewFileName[$i]     = $uploadNewFileNameWithoutExt . '.' . $extension;
            $uploadNewFileWithPathName = $uploadDirPath . $uploadNewFileName[$i];
            $mesUpload                 = uploadImageFileOnServer("imgName$i", $allowedExts, $maxFileSize, $uploadNewFileWithPathName);
        }
    }

    $itemImages = implode("#:#", $uploadNewFileName);

    $thumbnailImageName = "default_thumbnail.png";
    if ($isImageUploaded == 1) {
        $thumbnailImageName = $jpgFormatImageName;
    }

    if ($mesUpload == "FILE_UPLOADED") {
        $arrayResponse = array("response" => "OK", "message" => "OK UPLOAD SUCCESS");
        echo json_encode($arrayResponse);
        exit;
    } else {
        /* $mesUpload */
        $arrayResponse = array("response" => "FILE_FAILED", "message" => "FAIL TO UPLOAD");
        echo json_encode($arrayResponse);
        exit;
    }
    ?>

Here the screen shots of the error of that this is image where the

screen shot 1

screen shot 2

Please help me to solving this issue. i am puzzled for this error and i have not getting any idea to resolved this problem. If any one want to use i upload a sample code on the web click on below link https://tamapev.000webhostapp.com/upload-img/

The issue is probbaly you sending the form data to the wrong page, first determine whether action.php is in the root directory or if it is in a directory called "upload-img". Then send the request to that given page.

Next for the error that says "An error occurred trying to load resource", to see what the actual error is, in your first screenshot there is a little button in the panel that says "Response", Click on it and change it to "JSON"

If "action.php" is in "upload-img" then you need to change

url: "action.php", to url: "/upload-img/action.php",