NIC上传说“无效上传ID”,无法使其有效
题
我试图用nicupload插件实现nicedit,但是当我选择要上传的文件时,它会说“无法上传图像”,并且服务器响应说“无效上传ID”。
这是调用脚本并初始化的代码:
<script src="http://js.nicedit.com/nicEdit-latest.js" type="text/javascript"></script>
<script type="text/javascript">//<![CDATA[
bkLib.onDomLoaded(function() {
new nicEditor({uploadURI : '../../nicedit/nicUpload.php'}).panelInstance('area1');
});
//]]>
</script>
.
nicupload.php的路径是正确的,代码是可以在文档中找到的路径: http://nicedit.com/src/nicupload/nicupload.js
我使上载文件夹更改,并设置写入权限。根据文档( http://wiki.nedit.com/w/page/515 / configuration%20options ),即所有,但我不断得到错误。任何想法?
解决方案
您可以手动将ID传递给脚本:e.g nicupload.php?id= IntlOpicheader,它将成为您定义的图像文件夹中的IntrOnicheader.jpg(或适当的扩展)。 但是,我注意到,如果在NiceditorAdvancedButton.extend({。这会导致对相对路由的“未知”“资源中,则无法访问该脚本,并且如果在Nicedit.js中直接指定,则无法访问配置选项UploadUri。 。
文档否则否则,Nicuri在此用于Imgur.com(也许是默认值)的事实给予了我的印象,我也可以在单个地方而不是上的Nicupload.php脚本添加一个Uploaduri引用每个编辑实例化。
更新
如果您在实例化期间传递它,这是有效的,我猜是允许简单的动态ID群体。
不幸的是,nicupload.php被删除了错误,它的输出不是json。编辑器期望解析JSON并找到具有意外令牌“<”的脚本标记和错误。
我将尝试识别的其他错误的筏子:
在nicedit.js
- a.append(“图像”)应该是infact a.append(“nicimage”)
- this.onuploaded(d.upload)应该成为this.onuploaded(d)
- this.onuploaded(d)应该在var d= json.parse(c.responseetext)之后移动到try块中以修复变量范围问题
- b.image.width需要成为b.width
在nicupload.php 中
- JSON输出未正确形成,注释出HTML输出并输出只是JSON_ENCODE($ State)。
- JSON输出需要返回命名链接的键/值对,而不是URL,尽管重命名Var D= B.links to Var D= B.URL,但在Nicedit.js中也就是修复。
PHP和JavaScript代码留下了很多需要的批次,我经常得到很多错误,并一直在修复它们。
其他提示
在很长一段时间内寻找解决方案(没有真实解决方案的帖子),我现在自己修复了代码。我现在能够将图像上传到我自己的服务器。Thx到Firebug和Eclipse; - )
主要问题是nicupload.php旧的且不使用当前的Nicedit-Upload函数。
缺失是错误处理,随意添加此...
将 nienditor添加到您的PHP文件中,并配置它以使用nicedit.php:
.
new nicEditor({iconsPath : 'pics/nicEditorIcons.gif', uploadURI : 'script/nicUpload.php'}
下载 nicedit.js未压缩并在nicedit.js中更改以下行:
.
uploadFile : function() {
var file = this.fileInput.files[0];
if (!file || !file.type.match(/image.*/)) {
this.onError("Only image files can be uploaded");
return;
}
this.fileInput.setStyle({ display: 'none' });
this.setProgress(0);
var fd = new FormData();
fd.append("image", file);
fd.append("key", "b7ea18a4ecbda8e92203fa4968d10660");
var xhr = new XMLHttpRequest();
xhr.open("POST", this.ne.options.uploadURI || this.nicURI);
xhr.onload = function() {
try {
var res = JSON.parse(xhr.responseText);
} catch(e) {
return this.onError();
}
//this.onUploaded(res.upload); // CHANGE HERE
this.onUploaded(res);
}.closure(this);
xhr.onerror = this.onError.closure(this);
xhr.upload.onprogress = function(e) {
this.setProgress(e.loaded / e.total);
}.closure(this);
xhr.send(fd);
},
.
onUploaded : function(options) {
this.removePane();
//var src = options.links.original; // CHANGE HERE
var src = options['url'];
if(!this.im) {
this.ne.selectedInstance.restoreRng();
//var tmp = 'javascript:nicImTemp();';
this.ne.nicCommand("insertImage", src);
this.im = this.findElm('IMG','src', src);
}
var w = parseInt(this.ne.selectedInstance.elm.getStyle('width'));
if(this.im) {
this.im.setAttributes({
src : src,
width : (w && options.image.width) ? Math.min(w, options.image.width) : ''
});
}
}
更改像这样 的nicupload.php
.<?php
/* NicEdit - Micro Inline WYSIWYG
* Copyright 2007-2009 Brian Kirchoff
*
* NicEdit is distributed under the terms of the MIT license
* For more information visit http://nicedit.com/
* Do not remove this copyright message
*
* nicUpload Reciever Script PHP Edition
* @description: Save images uploaded for a users computer to a directory, and
* return the URL of the image to the client for use in nicEdit
* @author: Brian Kirchoff <briankircho@gmail.com>
* @sponsored by: DotConcepts (http://www.dotconcepts.net)
* @version: 0.9.0
*/
/*
* @author: Christoph Pahre
* @version: 0.1
* @description: different modification, so that this php file is working with the newest nicEdit.js (needs also modification - @see)
* @see http://stackoverflow.com/questions/11677128/nicupload-says-invalid-upload-id-cant-make-it-works
*/
define('NICUPLOAD_PATH', '../images/uploadedImages'); // Set the path (relative or absolute) to
// the directory to save image files
define('NICUPLOAD_URI', '../images/uploadedImages'); // Set the URL (relative or absolute) to
// the directory defined above
$nicupload_allowed_extensions = array('jpg','jpeg','png','gif','bmp');
if(!function_exists('json_encode')) {
die('{"error" : "Image upload host does not have the required dependicies (json_encode/decode)"}');
}
if($_SERVER['REQUEST_METHOD']=='POST') { // Upload is complete
$file = $_FILES['image'];
$image = $file['tmp_name'];
$id = $file['name'];
$max_upload_size = ini_max_upload_size();
if(!$file) {
nicupload_error('Must be less than '.bytes_to_readable($max_upload_size));
}
$ext = strtolower(substr(strrchr($file['name'], '.'), 1));
@$size = getimagesize($image);
if(!$size || !in_array($ext, $nicupload_allowed_extensions)) {
nicupload_error('Invalid image file, must be a valid image less than '.bytes_to_readable($max_upload_size));
}
$filename = $id;
$path = NICUPLOAD_PATH.'/'.$filename;
if(!move_uploaded_file($image, $path)) {
nicupload_error('Server error, failed to move file');
}
$status = array();
$status['done'] = 1;
$status['width'] = $size[0];
$rp = realpath($path);
$status['url'] = NICUPLOAD_URI ."/".$id;
nicupload_output($status, false);
exit;
}
// UTILITY FUNCTIONS
function nicupload_error($msg) {
echo nicupload_output(array('error' => $msg));
}
function nicupload_output($status, $showLoadingMsg = false) {
$script = json_encode($status);
$script = str_replace("\\/", '/', $script);
echo $script;
exit;
}
function ini_max_upload_size() {
$post_size = ini_get('post_max_size');
$upload_size = ini_get('upload_max_filesize');
if(!$post_size) $post_size = '8M';
if(!$upload_size) $upload_size = '2M';
return min( ini_bytes_from_string($post_size), ini_bytes_from_string($upload_size) );
}
function ini_bytes_from_string($val) {
$val = trim($val);
$last = strtolower($val[strlen($val)-1]);
switch($last) {
// The 'G' modifier is available since PHP 5.1.0
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
function bytes_to_readable( $bytes ) {
if ($bytes<=0)
return '0 Byte';
$convention=1000; //[1000->10^x|1024->2^x]
$s=array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB');
$e=floor(log($bytes,$convention));
return round($bytes/pow($convention,$e),2).' '.$s[$e];
}
?>