用EasyWebSever实现文件上传

linux用busybox里边的httpd可以加一个简单脚本实现上传,可以说是最小巧的http服务器了。windows里边可以用hfs,但是感觉还是有时候反应慢,显臃肿。后来发现一个更小的http服务器EasyWebSever,60多kb,支持cgi,php之类的。当然也可以用python,nodejs做简单的绿色服务器,但还是麻烦些。EasyWebSever实现上传可以加php,但是下载php就几十兆,也可以加python,nodejs,都是需要的东西太大。

所以我想用js脚本去实现,解释程序windows自带,用个1k的脚本就行了。鼓捣半天发现js不支持二进制,最后没办法,改成了autoit3脚本。脚本参考的httpd_post_upload.cgi « networking - busybox - BusyBox: The Swiss Army Knife of Embedded Linux

EasyWebSever服务器设置映射--扩展名au3,执行程序“D:\PCAPPS\autoit3\AutoIt3.exe d:\tmp\1.au3”,就是用1.au3当解释程序。用autoit3.exe当解释程序的话,因为服务器调用时不直接传递参数 ,而是靠环境变量传递,所以不能用。

index.htm的代码:

<html>
 <body>
 <form action=/2.au3 method=post enctype=multipart/form-data>
 File to upload: <input type=file name=file1> <input type=submit>
 </form>

---------------------------------------------------------------------------------------------------------------------------------

1.au3的代码:

ConsoleWrite(Binary("0x0a0d0a0d"))
$script_name=EnvGet("SCRIPT_NAME")
$oexec=run("D:\PCAPPS\autoit3\autoit3   d:\tmp\" & $script_name,@WorkingDir, @SW_HIDE, 7)

$aa=Binary("")
while 1
 $cc = ConsoleRead(0,1)
    If @error Then ExitLoop
    $aa=$aa & $cc
WEnd

StdinWrite($oexec,BinaryLen($aa) & chr(13))
StdinWrite($oexec,$aa )

$bb=Binary("")
while 1
 $cc = StdoutRead($oexec,0,1)
    If @error Then ExitLoop
    $bb=$bb & $cc
WEnd

ConsoleWrite($bb)

--------------------------------------------------------------------------------------------------------------------------------

2.au3的代码:

$aa=Binary("")
$count=0
$countok=0
$ii=0
while 1
 $cc = ConsoleRead(0,1)
    If @error Then ExitLoop    
    if @extended >0 then
       $aa=$aa & $cc
       $count=$count+@extended
       if  $count>20  then
            if  $countok=0 then
                $tt=BinaryMid($aa,1,20)
                $ttt=BinaryToString($tt)
                $ii=StringInStr($ttt,chr(13))
    $ccc=StringLeft($ttt,$ii)
                $countok=1
            endif
            if  $count>=$ccc+$ii Then ExitLoop
       endif
    endif
WEnd

for $i=$ii+1 to 2000
  if  BinaryMid($aa,$i,1)= chr(13) Then ExitLoop
next
$fengexian=BinaryMid($aa,$ii+1,$i-$ii-1)
for $iii=$i+1 to 2000
  if  BinaryMid($aa,$iii,2)= chr(10) & chr(13) Then ExitLoop
next

$ff=FileOpen("tmp",2)
FileWrite($ff,BinaryMid($aa,$iii+3,BinaryLen($aa)-$iii-3-BinaryLen($fengexian)-5))
FileClose($ff)

$dd=BinaryMid($aa,BinaryLen($aa)-BinaryLen($fengexian)-3,BinaryLen($fengexian))
if $fengexian = $dd  then 
ConsoleWrite("ok")
else
ConsoleWrite("err")
endif

-------------------------------------------------------------------------------------------------------------------------

最后我把js的代码也列出来,1.js:

var WshShell = WScript.CreateObject("WScript.Shell");
var WshSysEnv = WshShell.Environment("PROCESS");
script_name=WshSysEnv("SCRIPT_NAME")

oexec=WshShell.exec("cscript /nologo d:\\tmp\\"+script_name)

aa=WScript.StdIn.readall()


WScript.StdOut.Write("\r\n")
WScript.StdOut.Write("\r\n")


oexec.StdIn.Write(aa.length+"\n")
oexec.StdIn.Write(aa );
WScript.StdOut.Write(oexec.StdOut.readall())
------------------------------------------------------------------------------------------------------------------------------

2.js

count=WScript.StdIn.readLine()  //如果不用count计数,程序会一直阻塞,因为不能确定管道的终点
fengexian=WScript.StdIn.readLine()
count-=fengexian.length+2    //如果浏览器发送的换行是一个字符,程序会出问题

while (true){
bb=WScript.StdIn.readLine()
count-=bb.length+2
if (bb=="\r\n" ||  bb=="")break;
}

aa=WScript.StdIn.read(count-fengexian.length-6)
fso = new ActiveXObject("Scripting.FileSystemObject")
f = fso.CreateTextFile("d:\\tmp\\tmp", true)
f.Write(aa);
f.Close();

WScript.StdIn.read(2)
cc=WScript.StdIn.read(fengexian.length)
if(fengexian==cc)WScript.echo ("ok")
else WScript.echo ("err")
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值