我正在尝试上传文件和该文件的缩略图(使用cropper v2.3.0)。 该代码可在所有其他浏览器上使用,但在野生动物园中会出现错误。
问题描述如下:
在桌面上的safari浏览器上上传文件时,发生以下错误,并使用了其他错误,然后safari浏览器没有错误并获得成功消息。
我测试了两种方法,它们首先仅上传base64中的裁剪图像 编码,或者也可以将blob作为文件添加到formData中,但是在两种方式下均无法解决错误。
我也只尝试上传图像,然后有时会或有时不会发生错误。
如果使用裁纸器调整,则会发生错误(这是我的假设)
我的js代码提交表单
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;
}
我的动作php代码
<?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;
}
?>
以下是该错误的屏幕截图 这是
请帮助我解决此问题。我为这个错误感到困惑,我还没有任何想法来解决这个问题。 如果有人要使用,我在网上上传了示例代码,请点击以下链接 https://tamapev.000webhostapp.com/upload-img/
答案 0 :(得分:0)
问题可能是您将表单数据发送到错误的页面,首先确定action.php是在根目录中还是在名为“ upload-img”的目录中。然后将请求发送到给定的页面。
接下来显示“尝试加载资源时发生错误”的错误,以查看实际错误,在第一个屏幕截图中,面板上有一个小按钮,显示“ Response”,单击并更改改为“ JSON”
如果“ action.php”位于“ upload-img”中,则需要更改
url: "action.php",
至url: "/upload-img/action.php",