import win.ui;
import thread.command;
import thread.manage;
import fsys.dlg;
/*DSG{{*/
mainForm = win.form(text="ftp上传测试";right=759;bottom=469)
mainForm.add(
btnSwitch={cls="button";text="开始";left=656;top=24;right=736;bottom=128;disabled=1;dr=1;dt=1;font=LOGFONT(name='思源黑体 Normal';h=-19;weight=350);z=7};
btnUpload={cls="button";text="选择文件";left=552;top=24;right=648;bottom=128;dr=1;dt=1;font=LOGFONT(name='思源黑体 Normal';h=-19;weight=350);z=2};
strProgress={cls="static";left=456;top=96;right=544;bottom=127;dr=1;dt=1;font=LOGFONT(h=-16);transparent=1;z=6};
txtLocalFile={cls="static";text="本地文件:";left=24;top=32;right=528;bottom=56;aw=1;dl=1;dt=1;transparent=1;z=3};
txtRemoteFile={cls="static";text="远程文件:";left=24;top=64;right=528;bottom=88;aw=1;dl=1;dt=1;transparent=1;z=5};
uploadList={cls="listview";left=24;top=144;right=736;bottom=448;ah=1;asel=false;aw=1;db=1;dl=1;dr=1;dt=1;edge=1;font=LOGFONT(name='Source Sans Pro Semibold';h=-16;weight=600);fullRow=1;gridLines=1;msel=false;z=4};
uploadProgress={cls="progress";left=24;top=96;right=440;bottom=126;aw=1;dl=1;dr=1;dt=1;edge=1;max=100;min=0;z=1}
)
/*}}*/
threadUpload=function(host,username,password,uploadList){
import inet.ftp;
import thread.command;
import fsys.dlg;
import fsys.file;
import fsys;
ftpConn=inet.ftp(host,username,password); //创建FTP连接
min,fileCount=table.range(uploadList); //获取uploadList计数,用table.count有时会不准
for(i=1;fileCount;1){
var localFile=uploadList[i][3];
var remoteFile=uploadList[i][4];
var fid=tonumber(uploadList[i][1]);
thread.command.uploadStatus(fid,"开始上传"); //使用thread.command向主线程传递消息
var localFileHandle = io.open(localFile,"rb"); //打开本地文件
var remoteFileHandle = ftpConn.open("/"++remoteFile,"wb"); //打开FTP远端文件
thread.command.localFile(localFile); //使用thread.command向主线程传递消息
thread.command.remoteFile(remoteFile); //使用thread.command向主线程传递消息
var buffer = raw.malloc(1000);//按照1000字节分块上传
var uploadSum =0;
var localSize=tonumber(localFileHandle.size64()); //获取本地文件大小
jindu=0;
while(var size; size = localFileHandle.readBuffer(buffer);size) //按照buffer大小循环读取文件同时上传
{
remoteFileHandle.writeBuffer(buffer,size);
uploadSum = uploadSum +tonumber(fsys.formatSize(size));
var uploadPercent = string.format("%.2f",uploadSum/localSize*100);
thread.command.setProgress(uploadPercent) //使用thread.command向主线程传递消息,设置进度条进度
}
thread.command.uploadStatus(fid,"上传成功");
localFileHandle.close();
remoteFileHandle.close();
}
}
mainFormInitial=function(){
mainForm.uploadList.insertColumn("序号",50,,0x2/*_LVCFMT_CENTER*/);
mainForm.uploadList.insertColumn("状态",120,,0x2/*_LVCFMT_CENTER*/);
mainForm.uploadList.insertColumn("本地文件",300,,0x2/*_LVCFMT_CENTER*/);
mainForm.uploadList.insertColumn("远程文件",300,,0x2/*_LVCFMT_CENTER*/);
}
mainForm.btnUpload.oncommand = function(id,event){
mainForm.uploadList.items={};
uploadFileList=fsys.dlg.openEx("所有文件|*.*");
for(fid,filePath in uploadFileList){
mainForm.uploadList.addItem({fid;"等待";filePath;fsys.getFileName(filePath)});
}
mgr=thread.manage(1); //创建线程管理器
mgr.create(threadUpload,"192.168.1.1","9999","636363",mainForm.uploadList.items).onEnd = function(...){
mainForm.msgbox("上传完成!"); //创建线程并设置回调
mainForm.btnSwitch.text="开始"
mainForm.btnSwitch.disabled=true;
}
mgr.suspend(); //创建后先行暂停 点击开始后 线程再继续执行。
mainForm.btnSwitch.disabled=false;
mgr.waitClose() //等待线程关闭
}
mainForm.btnSwitch.oncommand = function(id,event){
//mainForm.msgbox( mainForm.btnSwitch.text );
if(mainForm.btnSwitch.text="开始"){
mgr.resume(); //继续执行线程
mainForm.btnSwitch.text="暂停"
}else{
mgr.suspend(); //继续执行线程
mainForm.btnSwitch.text="开始"
}
}
var listener = thread.command(); //设置监听操作,接受线程消息
listener.localFile = function( strLocalFile ){
mainForm.txtLocalFile.text="本地文件:" ++ strLocalFile
}
listener.remoteFile = function( strRemoteFile ){
mainForm.txtRemoteFile.text="远程文件:" ++ strRemoteFile
}
listener.setProgress = function( pos ){
mainForm.uploadProgress.pos=pos;
mainForm.strProgress.text=pos++"%";
}
listener.uploadStatus=function(fid,strStatus){
mainForm.uploadList.setItemText(strStatus,fid,2);
mainForm.uploadList.ensureVisible(fid);
}
mainFormInitial();
mainForm.enableDpiScaling();
mainForm.show();
win.loopMessage();