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();
|