当前位置: 代码迷 >> ASP >> ASP环境,百度Ueditor 无法下传附件,提示405异常或者404异常解决方法
  详细解决方案

ASP环境,百度Ueditor 无法下传附件,提示405异常或者404异常解决方法

热度:190   发布时间:2012-08-31 12:55:03.0
ASP环境,百度Ueditor 无法上传附件,提示405错误或者404错误解决办法。

UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于BSD协议,允许自由使用和修改代码...

在Ueditor1.2.0.0和之前的版本都支持asp,但是附件上传是无法使用的,通过查看代码后发现,Ueditor根本就没有提供asp环境下的附件上传功能,百度这么大的公司也有不厚道的时候,难道Ueditor开发组是为了从安全角度考虑而放弃了这个功能?不管什么原因,只好自己弄一个,解决办法如下:

1、找到Ueditor所在目录下的dialogs\attachment\attachment.html,打开后找到里面的

upload_url:"../../server/upload/php/fileUp.php"

改为:

upload_url:"../../server/upload/asp/FileUp.asp"

附上attachment.html文件,如果你找不到attachment.html文件,那就Copy一下吧,内容如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>附件上传</title>
    <meta http-equiv="Content-Type" content="text/html;charset=gbk"/>
    <link rel="stylesheet" type="text/css" href="attachment.css"/>
</head>
<body>
<div class="wrapper">
    <div class="controller">
        <span id="divStatus">本次共成功上传 0 个文件</span>
        <span id="spanButtonPlaceHolder"></span>
    </div>
    <div class="fieldset flash" id="fsUploadProgress"></div>
    <span id="startUpload" style="display: none;"></span>

</div>
<script type="text/javascript" src="../internal.js"></script>

<script type="text/javascript" src="../../third-party/swfupload/swfupload.js"></script>
<script type="text/javascript" src="../../third-party/swfupload/swfupload.queue.js"></script>
<script type="text/javascript" src="../../third-party/swfupload/fileprogress.js"></script>
<script type="text/javascript" src="callbacks.js"></script>
<script type="text/javascript" src="fileTypeMaps.js"></script>
<script type="text/javascript">
    var swfupload,
        filesList=[];
    window.onload = function () {
        var settings = {
            flash_url:"../../third-party/swfupload/swfupload.swf",
            flash9_url:"../../third-party/swfupload/swfupload_fp9.swf",
            upload_url:"../../server/upload/asp/FileUp.asp",           //附件上传服务器地址
            post_params:{"PHPSESSID":"<?php echo session_id(); ?>"}, //解决session丢失问题
            file_size_limit:"100 MB",                                 //文件大小限制,此处仅是前端flash选择时候的限制,具体还需要和后端结合判断
            file_types:"*.*",                                         //允许的扩展名,多个扩展名之间用分号隔开,支持*通配符
            file_types_description:"All Files",                      //扩展名描述
            file_upload_limit:100,                                   //单次可同时上传的文件数目
            file_queue_limit:10,                                      //队列中可同时上传的文件数目
            custom_settings:{                                         //自定义设置,用户可在此向服务器传递自定义变量
                progressTarget:"fsUploadProgress",
                startUploadId:"startUpload"
            },
            debug:false,

            // 按钮设置
            button_image_url:"../../themes/default/images/fileScan.png",
            button_width:"100",
            button_height:"25",
            button_placeholder_id:"spanButtonPlaceHolder",
            button_text:'<span class="theFont">文件浏览…</span>',
            button_text_style:".theFont { font-size:14px;}",
            button_text_left_padding:10,
            button_text_top_padding:4,

            // 所有回调函数 in handlers.js
            swfupload_preload_handler:preLoad,
            swfupload_load_failed_handler:loadFailed,
            file_queued_handler:fileQueued,
            file_queue_error_handler:fileQueueError,
            //选择文件完成回调
            file_dialog_complete_handler:function(numFilesSelected, numFilesQueued) {
                var me = this;        //此处的this是swfupload对象
                if (numFilesQueued > 0) {
                    dialog.buttons[0].setDisabled(true);
                    var start = $G(this.customSettings.startUploadId);
                    start.style.display = "";
                    start.onclick = function(){
                        me.startUpload();
                        start.style.display = "none";
                    }
                }
            },
            upload_start_handler:uploadStart,
            upload_progress_handler:uploadProgress,
            upload_error_handler:uploadError,
            upload_success_handler:function (file, serverData) {
                try{
                    var info = eval("("+serverData+")");
                }catch(e){}
                var progress = new FileProgress(file, this.customSettings.progressTarget);
                if(info.state=="SUCCESS"){
                    progress.setComplete();
                    progress.setStatus("<span style='color: #0b0;font-weight: bold'>上传成功!</span>");
                    filesList.push({url:info.url,type:info.fileType});
                    progress.toggleCancel(true,this,"从成功队列中移除");
                }else{
                    progress.setError();
                    progress.setStatus(info.state);
                    progress.toggleCancel(true,this,"移除保存失败文件");
                }

            },
            //上传完成回调
            upload_complete_handler:uploadComplete,
            //队列完成回调
            queue_complete_handler:function(numFilesUploaded){
                dialog.buttons[0].setDisabled(false);
                var status = $G("divStatus");
                var num = status.innerHTML.match(/\d+/g);
                status.innerHTML = "本次共成功上传 "+((num && num[0] ?parseInt(num[0]):0) + numFilesUploaded) +" 个文件" ;
            }
        };
        swfupload = new SWFUpload( settings );
        //点击OK按钮
        dialog.onok = function(){
            var map = fileTypeMaps,
                str="";
            for(var i=0,ci;ci=filesList[i++];){
                var src = editor.options.UEDITOR_HOME_URL + "dialogs/attachment/fileTypeImages/"+(map[ci.type]||"icon_default.png");
                str += "<p style='line-height: 16px;'><img src='"+ src + "' data_ue_src='"+src+"' />" +
                       "<a href='"+editor.options.filePath + ci.url+"'>" + ci.url + "</a></p>";
            }
            editor.execCommand("insertHTML",str);
            swfupload.destroy();
        };
        dialog.oncancel = function(){
            swfupload.destroy();
        }
    };
</script>

</body>
</html>

2、在Ueditor所在目录下的server/upload/asp新建一个文件FileUp.asp,内容如下:

<!--#include file="upload.inc"-->
<%dim upload,file,formName,formPath,filename,fileExt
dim maxSize
maxSize = 5000000	'允许上传的文件最大容量

set upload=new upload_5xSoft ''建立上传对象
formPath="/uploadFile/" '文件上传存放路径,这个目录在你网站上必须存在,否则会出错。

for each formName in upload.file ''列出所有上传了的文件
	set file=upload.file(formName)  ''生成一个文件对象

	fileExt=lcase(right(file.filename,4))

	'定义可上传的文件类型
	If fileEXT<>".doc" and fileEXT<>".txt" and fileEXT<>".zip" and fileEXT<>".rar" and fileEXT<>".pdf" and fileEXT<>".xls" then
 		response.write "{'state':'ERROR!文件类型错误!','url':'"&file.filename&"','fileType':'"&fileExT&"'}"
		response.end
	End If 
 
	randomize
	ranNum=int(90000*rnd)+10000
	filename=formPath&year(now)&month(now)&day(now)&ranNum&fileExt   '文件名称
 
	If file.FileSize>0 or file.filesize<maxSize then
		file.SaveAs Server.mappath(FileName)   '保存文件
	Else
		response.write "{'state':'ERROR!文件容量限制!','url':'"&file.filename&"','fileType':'"&fileExT&"'}"
		response.end
	End If
	set file=nothing
next
set upload=nothing  ''删除此对象%>
{'state':'SUCCESS','url':'<%=filename%>','fileType':'<%=fileEXT%>'}

 

3、如果Ueditor所在目录下的server/upload/asp目录下没有upload.inc文件,那么你要手工建一个,附上upload.inc文件内容

<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
    dim upfile_5xSoft_Stream

    Class upload_5xSoft

    dim Form,File,Version

    Private Sub Class_Initialize
    dim iStart,iFileNameStart,iFileNameEnd,iEnd,vbEnter,iFormStart,iFormEnd,theFile
    dim strDiv,mFormName,mFormValue,mFileName,mFileSize,mFilePath,iDivLen,mStr
    Version=""
    if Request.TotalBytes<1 then Exit Sub
    set Form=CreateObject("Scripting.Dictionary")
    set File=CreateObject("Scripting.Dictionary")
    set upfile_5xSoft_Stream=CreateObject("Adodb.Stream")
    upfile_5xSoft_Stream.mode=3
    upfile_5xSoft_Stream.type=1
    upfile_5xSoft_Stream.open
    upfile_5xSoft_Stream.write Request.BinaryRead(Request.TotalBytes)

    vbEnter=Chr(13)&Chr(10)
    iDivLen=inString(1,vbEnter)+1
    strDiv=subString(1,iDivLen)
    iFormStart=iDivLen
    iFormEnd=inString(iformStart,strDiv)-1
    while iFormStart < iFormEnd
    iStart=inString(iFormStart,"name=""")
    iEnd=inString(iStart+6,"""")
    mFormName=subString(iStart+6,iEnd-iStart-6)
    iFileNameStart=inString(iEnd+1,"filename=""")
    if iFileNameStart>0 and iFileNameStart<iFormEnd then
    iFileNameEnd=inString(iFileNameStart+10,"""")
    mFileName=subString(iFileNameStart+10,iFileNameEnd-iFileNameStart-10)
    iStart=inString(iFileNameEnd+1,vbEnter&vbEnter)
    iEnd=inString(iStart+4,vbEnter&strDiv)
    if iEnd>iStart then
    mFileSize=iEnd-iStart-4
    else
    mFileSize=0
    end if
    set theFile=new FileInfo
    theFile.FileName=getFileName(mFileName)
    theFile.FilePath=getFilePath(mFileName)
    theFile.FileSize=mFileSize
    theFile.FileStart=iStart+4
    theFile.FormName=FormName
    file.add mFormName,theFile
    else
    iStart=inString(iEnd+1,vbEnter&vbEnter)
    iEnd=inString(iStart+4,vbEnter&strDiv)

    if iEnd>iStart then
    mFormValue=subString(iStart+4,iEnd-iStart-4)
    else
    mFormValue=""
    end if
    form.Add mFormName,mFormValue
    end if

    iFormStart=iformEnd+iDivLen
    iFormEnd=inString(iformStart,strDiv)-1
    wend
    End Sub

    Private Function subString(theStart,theLen)
    dim i,c,stemp
    upfile_5xSoft_Stream.Position=theStart-1
    stemp=""
    for i=1 to theLen
    if upfile_5xSoft_Stream.EOS then Exit for
    c=ascB(upfile_5xSoft_Stream.Read(1))
    If c > 127 Then
    if upfile_5xSoft_Stream.EOS then Exit for
    stemp=stemp&Chr(AscW(ChrB(AscB(upfile_5xSoft_Stream.Read(1)))&ChrB(c)))
    i=i+1
    else
    stemp=stemp&Chr(c)
    End If
    Next
    subString=stemp
    End function

    Private Function inString(theStart,varStr)
    dim i,j,bt,theLen,str
    InString=0
    Str=toByte(varStr)
    theLen=LenB(Str)
    for i=theStart to upfile_5xSoft_Stream.Size-theLen
    if i>upfile_5xSoft_Stream.size then exit Function
    upfile_5xSoft_Stream.Position=i-1
    if AscB(upfile_5xSoft_Stream.Read(1))=AscB(midB(Str,1)) then
    InString=i
    for j=2 to theLen
    if upfile_5xSoft_Stream.EOS then
    inString=0
    Exit for
    end if
    if AscB(upfile_5xSoft_Stream.Read(1))<>AscB(MidB(Str,j,1)) then
    InString=0
    Exit For
    end if
    next
    if InString<>0 then Exit Function
    end if
    next
    End Function

    Private Sub Class_Terminate
    form.RemoveAll
    file.RemoveAll
    set form=nothing
    set file=nothing
    upfile_5xSoft_Stream.close
    set upfile_5xSoft_Stream=nothing
    End Sub


    Private function GetFilePath(FullPath)
    If FullPath <> "" Then
    GetFilePath = left(FullPath,InStrRev(FullPath, "\"))
    Else
    GetFilePath = ""
    End If
    End function

    Private function GetFileName(FullPath)
    If FullPath <> "" Then
    GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)
    Else
    GetFileName = ""
    End If
    End function

    Private function toByte(Str)
    dim i,iCode,c,iLow,iHigh
    toByte=""
    For i=1 To Len(Str)
    c=mid(Str,i,1)
    iCode =Asc(c)
    If iCode<0 Then iCode = iCode + 65535
    If iCode>255 Then
    iLow = Left(Hex(Asc(c)),2)
    iHigh =Right(Hex(Asc(c)),2)
    toByte = toByte & chrB("&H"&iLow) & chrB("&H"&iHigh)
    Else
    toByte = toByte & chrB(AscB(c))
    End If
    Next
    End function
    End Class


    Class FileInfo
    dim FormName,FileName,FilePath,FileSize,FileStart
    Private Sub Class_Initialize
    FileName = ""
    FilePath = ""
    FileSize = 0
    FileStart= 0
    FormName = ""
    End Sub

    Public function SaveAs(FullPath)
    dim dr,ErrorChar,i
    SaveAs=1
    if trim(fullpath)="" or FileSize=0 or FileStart=0 or FileName="" then exit function
    if FileStart=0 or right(fullpath,1)="/" then exit function
    set dr=CreateObject("Adodb.Stream")
    dr.Mode=3
    dr.Type=1
    dr.Open
    upfile_5xSoft_Stream.position=FileStart-1
    upfile_5xSoft_Stream.copyto dr,FileSize
    dr.SaveToFile FullPath,2
    dr.Close
    set dr=nothing
    SaveAs=0
    end function
    End Class 
</SCRIPT>


4、关于安全问题补充:由于FileUp.asp未加入权限验证,所以是不安全的,因此建议在FileUp.asp文件里面要加入权限验证,例如用session验证。

 

以上解决办法在Ueditor1.2.0.0版本上验证成功!同时附件下载地址:
2012-03-29 [1.2.0.0 版本] UTF-8版 GBK版

  相关解决方案