nic上传说"Invalid Upload 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.nicedit.com/w/page/515/Configuration%20Options),仅此而已,但我总是犯错误。有什么想法吗?

在寻找解决方案很长时间后(很多帖子都没有真正的解决方案),我现在自己修复了代码。我现在可以把图片上传到我自己的服务器上了。由于萤火虫和日食;-)

主要问题是nicUpload.php是旧的,不能使用当前的nicEdit Upload功能。

缺少的是错误处理,请随意添加

nicEditor添加到您的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];
}
?>

您可以手动将id传递给脚本:例如nicUpload.php?id=introPicHeader,它将在您定义的images文件夹中变为introPicHeader.jpg(或适当的扩展名)。

然而,我注意到这个脚本已经损坏,如果在nicEditorAdvancedButton.extend({.这会导致访问相对路径的"未知"资源,从而导致错误)期间直接在nicEdit.js中指定,则无法访问配置选项uploadURI

文档暗示了其他情况,而且这里为imgur.com指定了nicURI(可能是默认值),这给我的印象是,我还可以在一个地方添加对nicUpload.php脚本的uploadURI引用,而不是在每个编辑器实例化上。

更新

如果您在实例化过程中传递它,这将起作用,我想这确实允许轻松的动态id填充。

不幸的是,nicUpload.php充满了错误,并且它的输出不是JSON。编辑器期望解析JSON,并发现一个脚本标记和带有意外标记"<"的错误。

还有一大堆其他错误,我将尝试识别:

在nicEdit.js 中

  1. A.append("image")应该是实际的A.append
  2. this.onUploaded(D.upload)应该变成this.onUploaded(D)
  3. this.onUploaded(D)应移到var D=JSON.parse(C.responseText)之后的try块中以修复变量范围问题
  4. B.image.width需要变成B.width

在nicUpload.php 中

  1. JSON输出格式不正确,注释掉html输出,只输出JSON_encode($status)
  2. JSON输出需要返回一个名为links而不是url的键/值对,尽管在nicEdit.js中将var D=B.links重命名为var D=B.url也足够了

php和javascript代码都有很多不足之处,我经常会遇到很多错误,并且一直在自己修复它们。

相关内容

  • 没有找到相关文章

最新更新