|
之前看了版主的web.rest帖子http://bbs.aardio.com/forum.php? ... ;highlight=web.rest
写了个关于web.rest上传文件的demo,分享给大家,代码不是很精炼。有兴趣的可以整理。
demo中用的上传接口是公家的,不能公开给大家玩,大家换成自己的上传接口即可,谅解!
- import win.ui;
- import gdip;
- import com.picture;
- import fsys.dlg;
- import web.rest.client;
- import web.rest.jsonLiteClient;
- import web.json;
- /*DSG{{*/
- var mainForm = win.form(text="aardio form";right=583;bottom=505)
- mainForm.add(
- picturebox={cls="picturebox";left=24;top=37;right=260;bottom=143;bgcolor=16711680;image=$"E:\软件开发\aardio\aardio\掌聚\发布评论\aardio工程\res\images\30.jpg";notify=1;z=1};
- picturebox2={cls="picturebox";left=24;top=324;right=260;bottom=430;bgcolor=65280;image=$"E:\软件开发\aardio\aardio\掌聚\发布评论\aardio工程\res\images\sp.jpg";notify=1;z=11};
- progress={cls="progress";left=109;top=234;right=513;bottom=264;edge=1;max=100;min=0;z=2};
- static={cls="static";text="上传进度条:";left=25;top=239;right=98;bottom=262;transparent=1;z=6};
- static2={cls="static";text="上传成功返回的图片URL";left=25;top=163;right=181;bottom=186;transparent=1;z=8};
- static3={cls="static";text="上传成功返回的视频URL";left=19;top=451;right=175;bottom=474;transparent=1;z=9};
- static4={cls="static";text="上传图片";left=27;top=13;right=119;bottom=28;transparent=1;z=12};
- static5={cls="static";text="上传视频";left=24;top=290;right=116;bottom=305;transparent=1;z=13};
- stc_upimgzh={cls="static";left=374;top=89;right=492;bottom=110;align="center";transparent=1;z=4};
- stc_upjd={cls="static";left=375;top=38;right=493;bottom=59;align="center";transparent=1;z=3};
- stc_upzh={cls="static";left=372;top=357;right=490;bottom=378;align="center";transparent=1;z=5};
- txt_imgPath={cls="edit";left=176;top=163;right=511;bottom=185;edge=1;z=7};
- txt_resPath={cls="edit";left=168;top=448;right=503;bottom=470;edge=1;z=10}
- )
- /*}}*/
- //mainForm.progress.hide = true;
- mainForm.progress.max = 500;
- open_path = function(){
-
- var path=fsys.dlg.open("jpg|*.jpg|png|*.png|bmp|*.bmp|gif|*.gif");
- return path;
- }
- open_video_path = function(){
-
- var video_path=fsys.dlg.open("mp4|*.mp4");
- return video_path;
- }
- //上传文件
- upload_file = function(Path){
-
- mainForm.stc_upjd.text = 0; //算是个中介吧,记录每次上传了多少kb,累加用。
- var restClient = web.rest.jsonLiteClient();
-
- var restApi = restClient.api("http://www.xxxx.com/xxxxxxx/xxxxxxx/{oname}.json"); //大家用自己的上传接口吧,公家接口不能给大家玩。
-
- var token = string.load("D:\token.txt");//加载本地文件中的Cookie 或者 token
-
- restClient.addHeaders ="Rtoken:"++token;//此处向headers添加Cookie 或者 Token,本人项目token
-
-
- var str,sendSize,contentLenth = restClient.sendMultipartForm( {
-
- files = "@"+Path;
-
- },function(str,sendSize,contentLength){
-
- ..io.print("正在上传...",sendSize,contentLength);
-
- mainForm.progress.hide = false;
-
- var if_img_video = string.match(Path,"[\.]+[\a\d]+");//获取上传文件后缀,判断是图片,还是视频。
-
- //上传视频文件
- if(if_img_video = '.mp4' || if_img_video = '.MP4' || if_img_video = '.Mp4' || if_img_video = '.mP4'){
-
-
- if(sendSize == 0){
-
- var prpos = mainForm.progress.pos;
-
- if(prpos == 500){
-
- mainForm.stc_upzh.text = "视频上传成功";
-
- }
- }else {
-
- var num = mainForm.stc_upjd.text;
-
- mainForm.stc_upjd.text = sendSize + num;
-
- var jindu = num/contentLength * 500;//进度条
-
- mainForm.progress.pos = math.ceil(jindu);
-
- mainForm.stc_upzh.text = "视频正在上传...";
-
- }
-
- //上传图片文件
- } elseif (if_img_video = '.jpg' || if_img_video = '.png' || if_img_video = '.gif' || if_img_video = '.bmp'){
-
-
- if(sendSize == 0){
-
- var prpos = mainForm.progress.pos;
-
- if(prpos == 500){
-
- mainForm.stc_upimgzh.text = "图片上传成功";
-
- }
-
- }else {
-
- var num = mainForm.stc_upjd.text;
-
- mainForm.stc_upjd.text = sendSize + num;
-
- var jindu = num/contentLength * 500;//进度条
-
- mainForm.progress.pos = math.ceil(jindu);
-
- mainForm.stc_upimgzh.text = "图片正在上传...";
-
- }
-
- }
- win.delay(10)
-
- }
-
- );
- restApi.upload();
- return restClient.lastResponse();
-
- }
- mainForm.picturebox.oncommand = function(id,event){
-
- var imgPath = open_path();
-
- if(imgPath != "" && imgPath != null){
-
- mainForm.txt_imgPath.text = imgPath;
-
- var img = gdip.image (imgPath);
-
- mainForm.picturebox.image=imgPath;
-
- var wx = 236/img.width;
- var hx = 106/img.height;
-
- //凡是高度大于106的图片,按比例缩小显示,小于或者等于控件设定高度106的,直接显示原图
- if(img.height > 106){
-
- mainForm.picturebox.setPos( , ,img.width*wx,img.height*hx);
-
- }else {
-
- mainForm.picturebox.setPos( , ,width,height);
-
- }
-
- var imgUrl = upload_file(imgPath);
- var t = web.json.parse(imgUrl);
-
- mainForm.txt_imgPath.text = t.aaData[1].fileName;
-
- }
-
- }
- //上传视频
- mainForm.picturebox2.oncommand = function(id,event){
-
- var videopath = open_video_path();
-
- if(videopath != "" && videopath != null){
-
- mainForm.txt_resPath.text = videopath;
-
- var videw = upload_file(videopath);
-
- var ver = web.json.parse(videw);
-
- mainForm.txt_resPath.text = ver.aaData[1].fileName;
-
- }
-
- }
- mainForm.show()
- win.loopMessage();
复制代码
|
|