发新话题
打印

[转载]跨服务器上传文件最佳解决方案

[转载]跨服务器上传文件最佳解决方案

信息来源:黑基

如果你有两个空间,一个大而慢,另外一个小而快,或者其中一个不支持FSO,那么跨服务器上传文件这个问题就摆在你面前了,下面就是我在解决ylog.net里面的跨服务器上传文件的日记,这个问题看似简单,里面的细节问题却是非常有趣
前提条件,空间都必须支持ASP,上传文件的服务器支持FSO,下面的叙述中,diygame.com为存上传文件的服务器,ylog.net为网站服务器,即显示用户界面的服务器...
实现功能,文件上传,上传后在网页的文本区域自动加上对于图片的UBB码,为了清楚,下面列出所有用到的文件


服务器         文件名             用途
diygame.com    blog_upfile.asp         接收上传文件用
ylog.net           blog_add.asp          添加BLOG,上传文件功能在此出现
ylog.net           blog_upload.asp       上传文件表单,以iframe的形式嵌在blog_add.asp里
ylog.net           blog_upresult.asp      显示上传结果,作善后工作
ylog.net          blog_upcheck.asp      验证用户名与密码

_sunny.gif" width=1 border=0>
在发布BLOG页面Blog_add.asp增加一个iFrame,调用blog_upload.asp进行上传操作

<iframe border="0" frameBorder="0" frameSpacing="0" height="25" marginHeight="0" marginWidth="0" noResize scrolling="no" width="100%" vspale="0" src="blog_upload.asp"></iframe>
_sunny.gif" width=1 border=0>

如果你玩过ASP,自然不费吹灰之力想到,文件要传到另外一个服务器上,只要把blog_upload.asp里的上传Form的action页面指向目标服务器就行了,

<form name="form" method="post" action="http://www.diygame.com/blog_UPFILE.ASP"
//中间略去
//提交时把发布BLOG的按纽disable先,以免没传完就时用户把BLOG发表了
  <input type="submit" class=button name="Submit" value=" 上 传 " onclick="parent.document.frmAnnounce.Submit.disabled=true">
_sunny.gif" width=1 border=0>
测试一下,文件上传成功,那自动加UBB代码呢...
在blog_UPFILE.ASP" target=_blank>http://www.diygame.com/blog_UPFILE.ASP  里加一句js脚本

<script>parent.frmAnnounce.Content.value+=&#39;&#39;</script>
_sunny.gif" width=1 border=0>传上去再测试,错误提示"blog_UPFILE.ASP  权限不够",既然diygame.com的文件权限不够,ylog.net总可以吧,当blog_upfile.asp操作完成时,再调用一个本服务器的ASP文件,于是blog_upresult.asp出现了,他负责善后
修改blog_upfile.asp,我用C++的习惯,注释用//符


servername="www.ylog.net"  //标志服务器名
//检查来源,是否为自己指定的服务器,
if not instr(1,Request.ServerVariables("http_Referer"),servername,1)=8 then
    response.write "非法来源~!"
    response.end   
end if
//检查文件大小,类型,等,这些代码略去,网上很多关于FSO组件的介绍
...
//上传代码,略去
....
//成功后转向的URL,就是执行上传操作的地址,把信息当作msg传过去
url="http://"&servername&"/blog_upresult.asp?msg="
if  上传成功 then//把脚本传过去,因为JS中的+号不能被传递.因此使用server.urlencode函数,此脚本在ylog.net上才有权限运行
   url=url+ "<script>parent.frmAnnounce.Content.value"&server.urlencode("+")&"=&#39;&#39;</script>"
    //把文件名也传送过去,以便存数据库备查
    response.redirect  url+"上传成功&filename="&FileName
end if

_sunny.gif" width=1 border=0>
下面就是blog_upresult.asp上的代码了,很简单


//传成功了,自然要把已经diable的提交BLOG按纽恢复
<script>
parent.frmAnnounce.Submit.disabled=false;
</script>
//还有就是把传过来的信息显示出来
response.write request("msg")
//如果request("filename")<>"" 写入数据库代码省略
response.write "[ <a href=# onclick=history.go(-1)>重新上传</a> ]"

_sunny.gif" width=1 border=0>
看上去perfect了,但如人家得到了你的源码的话,轻而易举把你的上传服务器当成网络硬盘用....
只要把hosts文件里的中一句 127.0.0.1  www.ylog.net
然后相应写一个提交文件用的blog_upload.asp就行了,
头疼ing,代码是不能允许有半点安全漏洞的,验证的域名能被欺骗,
那就验证上传者的用户名与密码,diygame.com怎么去ylog.net的数据库上去查询用户名与密码是否正确呢
这就少不了xmlhttp
先在ylog.net上做一个blog_upcheck.asp,内容非常简单,对传的用户名与密码验证,成功则输出1,失败则输出0

<%
name=request("name")
psw=request("psw")
checkstr(name)//滤掉SQL字符
checkstr(psw))//滤掉SQL字符
  if  从数据库检查用户名=成功 then
    response.write 0
    else
    response.write 1
    end if

%>
_sunny.gif" width=1 border=0>
blog_upfile.asp接受上传之前先调用此文件验证.下面为代码,虽然也是使用域名www.ylog.net但此操作在diygame.com的服务器上执行,所以与使用者本地的hosts文件无关

<%
str=getHTTPPage("http://"&servername&"/blog_upcheck.asp?name="&name&"&psw="&password)
if str<>"1" then
    response.write "非法用户~!"
    response.end   
end if
//两个操作函数。非常有用,可以用到别的地方
Function getHTTPPage(URL)
   Set HTTPReq = Server.createobject("Microsoft.XMLHTTP")
   HTTPReq.Open "GET", URL, False
   HTTPReq.send
   If HTTPReq.readyState <> 4 Then Exit Function
   getHTTPPage = bytes2BSTR(HTTPReq.responseBody)
   Set HTTPReq = Nothing
End Function

Function bytes2BSTR(vIn)
   Dim strReturn
   Dim I, ThisCharCode, NextCharCode
   strReturn = ""
   For I = 1 To LenB(vIn)
      ThisCharCode = AscB(MidB(vIn, I, 1))
      If ThisCharCode < &H80 Then
        strReturn = strReturn & Chr(ThisCharCode)
      Else
        NextCharCode = AscB(MidB(vIn, I + 1, 1))
        strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
        I = I + 1
      End If
   Next
   bytes2BSTR = strReturn
End Function
%>
_sunny.gif" width=1 border=0>
做到这个地方,终于可以松口气了,写的很乱,希望能勉强看懂。。。

TOP

发新话题