aardio 官方社区

用户名  找回密码
 注册会员

QQ登录

只需一步,快速开始

帖子
查看: 8503|回复: 1

web.kit 上传文件

[复制链接]

166

主题

2154

回帖

1万

积分

管理员

积分
13056
发表于 2017-12-12 01:56:25 | 显示全部楼层 |阅读模式
经常有人问 web.kit  不能上传文件,
其实这是很久以前的事了,现在的版本早就支持上传文件了,直接看效果的源码:
import win.ui;
/*DSG{{*/
var winform = win.form(text="WebKit上传文件")
winform.add()
/*}}*/

import web.kit.form;
var wb = web.kit.form( winform  );

wb.html =
/**
<body>
    <form action="http://eu.httpbin.org/post" method="post" enctype="multipart/form-data">
        选择文件:<input type="file" name="file1" /><br />
        <input type="submit" value="上传" />
    </form>
</body>
**/


winform.show();
win.loopMessage();





166

主题

2154

回帖

1万

积分

管理员

积分
13056
 楼主| 发表于 2017-12-12 02:26:32 | 显示全部楼层
import win.ui;
/*DSG{{*/
var winform = win.form(text="webkit上传文件示例")
/*}}*/

import web.kit.form;  
var wb = web.kit.form(winform);  

import inet.http;
import web.multipartFormData;
import fsys.dlg;
wb.external = {

    upload =
function(userAgent,proc){
        
var postSize,contentLength = 0;
        
var filePath  = fsys.dlg.open(,"选择要上传的文件");
        
if(!filePath) return;
        
        
var form = web.multipartFormData();
        form.add(
"username","用户名");
        form.add(
"password","密码");
        form.addFile(
"filename",filePath);
        
        
//使用webkit的userAgent,假装是webkit客户端
        var http = inet.http(userAgent);
        http.disableCookies();
//禁用默认的cookie
        http.disableCache(); //强制刷新
        http.addHeaders ={
            cookie = wb.cookie;
//添加浏览器的cookie
        }
        
        http.onSendBegin =
function(){
            contentLength = form.size();
            
return contentLength;
        }
        http.onSend =
function(remainSize){
            
var str = form.read(0x2000);
            
if(!#str)return;
            postSize = postSize + #str;
            proc( contentLength,postSize,remainSize-#str );
            win.delay(10)
            
return str;
        }
        
var json = http.post("http://eu.httpbin.org/post",,{ ["Content-Type"] = form.contentType() }  );

        wb.document.write(
"<pre>"+json+"</pre>")
    };
}
wb.html =
/**
<!doctype html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
    <a href='javascript:external.upload(navigator.userAgent,
        function(contentLength,postSize,remainSize){
            document.write( "<br>总大小:" + contentLength +" 已上传:" + postSize + " 剩余:" + remainSize);
        });'>上传</a>
</body>
</html>
**/


winform.show();
win.loopMessage();
   
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

手机版|未经许可严禁引用或转载本站文章|aardio.com|aardio 官方社区 ( 皖ICP备09012014号 )

GMT+8, 2025-6-8 22:26 , Processed in 0.061324 second(s), 23 queries .

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表