dongzen7263 2018-12-19 11:55
浏览 767

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/

  • 写回答

1条回答 默认 最新

  • dsd30433 2018-12-19 12:17
    关注

    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",

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)