【发布时间】:2016-11-29 19:13:21
【问题描述】:
我正在尝试使用 Froala 所见即所得编辑器将图像上传到我的本地主机(用于测试目的),但它不起作用。当我选择要上传的图像时,它在编辑器中显示为褪色,然后在我单击其他位置时消失。下面是我的代码,以及我尝试关注的他们文档的链接。
文档: https://www.froala.com/wysiwyg-editor/docs/concepts/image/upload
也有这个方法,但是不知道把php放在哪里:https://www.froala.com/wysiwyg-editor/docs/sdks/php/image-server-upload
我的密码
(“fedit”是我用于文本区域的类。)
Javascript:
<script>
$(function() {
$('.fedit').froalaEditor({
// Set the image upload parameter.
imageUploadParam: 'file',
// Set the image upload URL.
imageUploadURL: '/upload_image.php',
// Additional upload params.
imageUploadParams: {class: 'fedit'},
// Set request type.
imageUploadMethod: 'POST',
// Set max image size to 5MB.
imageMaxSize: 5 * 1024 * 1024,
// Allow to upload PNG and JPG.
imageAllowedTypes: ['jpeg', 'jpg', 'png']
})
.on('froalaEditor.image.beforeUpload', function (e, editor, images) {
// Return false if you want to stop the image upload.
})
.on('froalaEditor.image.uploaded', function (e, editor, response) {
// Image was uploaded to the server.
})
.on('froalaEditor.image.inserted', function (e, editor, $img, response) {
// Image was inserted in the editor.
})
.on('froalaEditor.image.replaced', function (e, editor, $img, response) {
// Image was replaced in the editor.
})
.on('froalaEditor.image.error', function (e, editor, error, response) {
// Bad link.
else if (error.code == 1) { ... }
// No link in upload response.
else if (error.code == 2) { ... }
// Error during image upload.
else if (error.code == 3) { ... }
// Parsing response failed.
else if (error.code == 4) { ... }
// Image too text-large.
else if (error.code == 5) { ... }
// Invalid image type.
else if (error.code == 6) { ... }
// Image can be uploaded only to same domain in IE 8 and IE 9.
else if (error.code == 7) { ... }
// Response contains the original server response to the request if available.
});
});
</script>
上传图片.php
<?php
// Allowed extentions.
$allowedExts = array("gif", "jpeg", "jpg", "png", "blob");
// Get filename.
$temp = explode(".", $_FILES["file"]["name"]);
// Get extension.
$extension = end($temp);
// An image check is being done in the editor but it is best to
// check that again on the server side.
// Do not use $_FILES["file"]["type"] as it can be easily forged.
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES["file"]["tmp_name"]);
if ((($mime == "image/gif")
|| ($mime == "image/jpeg")
|| ($mime == "image/pjpeg")
|| ($mime == "image/x-png")
|| ($mime == "image/png"))
&& in_array(strtolower($extension), $allowedExts)) {
// Generate new random name.
$name = sha1(microtime()) . "." . $extension;
// Save file in the uploads folder.
move_uploaded_file($_FILES["file"]["tmp_name"], getcwd() . "/uploads/ " . $name);
// Generate response.
$response = new StdClass;
$response->link = "/uploads/" . $name;
echo stripslashes(json_encode($response));
}
?>
【问题讨论】:
-
这里没有足够的信息,你的 JS 控制台给你任何信息或者你的服务器上的错误日志怎么样。
-
感谢您的回复。我的日志中出现错误:加载资源失败:服务器响应状态为 404 (Not Found)localhost/froala_uploads 。然后当我尝试添加图像时出现另一个错误: image.min.js:7 GET localhost/froala_uploads 404 (Not Found)f @ image.min.js:7dispatch @ jquery.js:3r.handle @ jquery.js:3 .顺便说一句,我将文件夹更改为“froala_uploads”,只是为了更容易识别。
-
他们在他们的网站上提供了一个 SDK,你为什么不使用它?
-
不幸的是,我在编程方面真的没有那么先进。我非常了解 php,但从未完全理解 SDK 是什么。这是您使用的东西而不是“upload_image.php”,还是与它一起使用?froala.com/wysiwyg-editor/docs/sdks/php.
-
froala.com/wysiwyg-editor/docs/sdks/php/image-server-upload 我不确定是否应该将 php 代码放在一个名为 upload_image.php 的单独文件中,还是该文件仍应包含上述代码?
标签: javascript php ajax froala