.examples

git-svn-id: svn://ultimatepp.org/upp/trunk@15675 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2021-01-19 08:15:13 +00:00
parent 482aa6e1ed
commit df804bd0d0
7 changed files with 0 additions and 387 deletions

View file

@ -1,110 +0,0 @@
#include "SkylarkUpload.h"
int ProgressHandler(int reason, Http &http, int size)
{
// get the upload unique identifier
String id = http["uploadid"];
String currentId = "." + id + ".currentSize";
String totalId = "." + id + ".totalSize";
// must be reentrant
INTERLOCKED {
switch(reason)
{
// got headers ?
case PROGRESS_HEADER:
{
http
.SessionSet(currentId, 0)
.SessionSet(totalId, size)
;
break;
}
// reading contents ?
case PROGRESS_CONTENT:
{
int oldPercent = http[currentId];
int total = http[totalId];
// take care to NOT return 100% up to upload ended really
int percent = min(99, (int)(100.0 * size / total));
// avoid unnnedded session storing
if(oldPercent != percent)
http.SessionSet(currentId, percent);
break;
}
// finished reading contents ?
case PROGRESS_END:
{
// signals end by resetting total size
http.SessionSet(totalId, 0);
break;
}
// default, used by query handler
default: // PROGRESS_QUERY
{
// check if key is there --> upload started
int total = http[totalId];
if(!IsNull(total))
{
if(total)
// if upload not ended, return the progress %
return http[currentId];
else
{
// upload ended, nullify session variables and return 100%
http
.SessionSet(currentId, Null)
.SessionSet(totalId, Null)
;
return 100;
}
}
else
// upload still not started, return 0 progress
return 0;
}
}
}
return true;
}
SKYLARK(Home, "home")
{
// we need a session variable for upload id
if(http["@__skylark_session_cookie__"].IsNull())
http.NewSessionId();
// show rootpath in html page
http("UploadFolder", Upload().GetRootPath());
http.RenderResult("SkylarkUpload/SkylarkUpload");
}
SKYLARK(Default, "**")
{
http.Redirect(Home);
}
SKYLARK_PROGRESS(PostUpload, "upload:POST", &ProgressHandler)
{
String fileName = AppendFileName(Upload().GetRootPath(), (String)http["filename"]);
Value const &contents = http["filestoupload[]"];
Value const &filenames = http["filestoupload.filename[]"];
if(contents.IsNull() || filenames.IsNull())
return;
for(int i = 0; i < contents.GetCount(); i++)
SaveFile(AppendFileName(Upload().GetRootPath(), (String)filenames[i]), contents[i]);
}
SKYLARK(Progress,"progress")
{
int p = ProgressHandler(PROGRESS_QUERY, http, 0);
http.Content("text/plain", Format("%d", p));
http.Response(200, "OK");
}

View file

@ -1,33 +0,0 @@
#include "SkylarkUpload.h"
SkylarkUpload &Upload()
{
return (SkylarkUpload &)SkylarkApp::TheApp();
}
SkylarkUpload::SkylarkUpload(String const &rPath)
{
root = "server";
rootPath = rPath;
RealizeDirectory(rootPath);
#ifdef _DEBUG
prefork = 0;
use_caching = false;
#endif
}
CONSOLE_APP_MAIN
{
#ifdef _DEBUG
StdLogSetup(LOG_FILE|LOG_COUT);
Ini::skylark_log = true;
#endif
SkylarkUpload upload(AppendFileName(GetHomeDirectory(), "UPLOAD/"));
upload.Run();
}

View file

@ -1,36 +0,0 @@
/* body container -- used just to center on screen */
#bodyContainer {
text-align:center;
width:100%;
height:20em;
background-color:#eee;
}
/* a fancy header */
#header {
font-size:30px;
color:white;
background-color:blue;
}
/* progress bar container -- sets the maximum bar width */
#progress_container {
border: solid 1px #000;
height: 20px;
width: 300px;
text-align:center;
margin:0 auto;
}
/* progress bar */
#progress {
background-color: #006666;
color:white;
height: 20px;
width: 0px;
}
/* progress and hidden frame not showing */
#progress_container, #hidden_iframe {
display: none;
}

View file

@ -1,29 +0,0 @@
#ifndef _SkylarkUpload_SkylarkUpload_h_
#define _SkylarkUpload_SkylarkUpload_h_
#include <Skylark/Skylark.h>
using namespace Upp;
class SkylarkUpload : public SkylarkApp
{
private:
// root path for saving files
String rootPath;
protected:
public:
// constructor
SkylarkUpload(String const &rPath);
// get root path
String const &GetRootPath(void) { return rootPath; }
};
SkylarkUpload &Upload();
#endif

View file

@ -1,17 +0,0 @@
description "Skylark upload with ajax progress bar\377";
uses
Core,
Skylark;
file
SkylarkUpload.h,
Handlers.icpp,
SkylarkUpload.cpp,
SkylarkUpload.witz,
SkylarkUpload.css,
upload.js;
mainconfig
"" = "MT";

View file

@ -1,76 +0,0 @@
<html>
<head>
<title>File Upload Progress Bar</title>
<link rel='stylesheet' type='text/css' href='static/SkylarkUpload/SkylarkUpload.css'>
<!-- this script does all the upload magics -->
<script type='text/javascript' src='static/SkylarkUpload/upload.js'></script>
</head>
<body>
<div id='bodyContainer'>
<div id='header'>
Skylark file upload demo
</div>
<br>
<br>
This demo supports uploading multiple files at once
<br>
Files will be uploaded in '$UploadFolder' folder on server
<br>
<br>
<!--
POST URL -- javascript code will read it and build the 'action' of following form
You MUST insert in 'value' field the upload url (here 'upload')
-->
<input type='hidden' id='uploadurl' value='upload'>
<!--
progress handler URL -- javascript will use it to make periodic calls on server
querying upload progress and showing it in progress bar
You MUST insert in 'value' field the progress url (here 'progress')
-->
<input type='hidden' id='progressurl' value='progress'>
<!--
POST form
action is a dummy value, will be filled by javascript code
which sets the POST
-->
<form action='xxxxxx' method='POST' id='myForm' enctype='multipart/form-data' target='hidden_iframe' onsubmit='startUpload(this);'>
<!--
ending [] in filestoupload[] makes a ValueArray of multipart data
in Skylark arrays will be :
filestoupload[] a ValueArray with file contents
filestoupload.content_type[] a ValueArray with file content types
filestoupload.filename[] a ValueArray with filenames
The 'multiple' field allows select and upload more files at once
-->
<input type='file' name='filestoupload[]' multiple=''>
<br><br>
<input type='submit' value='Start Upload'>
<br>
</form>
<!-- Progress bar -->
<div id='progress_container'>
<div id='progress'></div>
</div>
<!--
hidden frame doing the upload process
it's used to 'free' the browser window during upload phase
-->
<iframe id='hidden_iframe' name='hidden_iframe' src='about:blank'></iframe>
<!--
these fields are used to build an unique identifier of this upload job
in order to allow progress handler to identify correctly its related upload job
(we need it because we can have more upload jobs calls on server
-->
<input type='hidden' id='session' value='$@__skylark_session_cookie__'>
<input type='hidden' id='submittime' value=''>
</div>
</body>
</html>

View file

@ -1,86 +0,0 @@
// toggles progress bar visibility
function toggleBarVisibility() {
var e = document.getElementById("progress_container");
e.style.display = (e.style.display == "block") ? "none" : "block";
}
// create http request object for upload
// browser dependent -- probably NOT completely cross browser
// if you use JQuery there are better ways
function createRequestObject() {
var http;
if (navigator.appName == "Microsoft Internet Explorer") {
http = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
http = new XMLHttpRequest();
}
return http;
}
function sendRequest() {
var http = createRequestObject();
// use session cookie and current time to build an unique upload identifier
var session = document.getElementById('session').value;
var time = document.getElementById('submittime').value;
// get upload url
var url = document.getElementById('progressurl').value;
// build GET uri and do the request
http.open("GET", url + "?uploadid=" + session + time);
// setup a callback handling end of request
http.onreadystatechange = function () { handleResponse(http); };
http.send(null);
}
// this callback handles 'progress' http requests results
function handleResponse(http) {
var response;
if (http.readyState == 4) {
response = http.responseText;
document.getElementById("progress").style.width = response + "%";
document.getElementById("progress").innerHTML = response + "%";
if (response < 100) {
// if upload still not complete, repost a progress request after some time
// (here 200 msec)
setTimeout("sendRequest()", 200);
}
else {
// if progress is complete, put 'Done' inside bar, then
// run a callback that hides it after some time
document.getElementById("progress").innerHTML = "100% Done.";
// hides 'Done' status
setTimeout("closeProgress()", 3000);
}
}
}
// this callback hides status bar after some time upload is over
function closeProgress() {
toggleBarVisibility();
document.getElementById("progress").innerHTML = "";
}
function startUpload(me) {
// sets an unique 'uploadid' field composed by session cookie and current time
var session = document.getElementById('session').value;
var time = Date.now();
document.getElementById('submittime').value = time;
// build upload URI adding the 'uploadid' variable
me.action = document.getElementById('uploadurl').value + '?uploadid=' + session + time;
// setup progress bar
document.getElementById("progress").style.width = 0;
document.getElementById("progress").innerHTML = "0%";
toggleBarVisibility();
// starts the delayed progress handler
setTimeout("sendRequest()", 200);
}