Wedge

Public area => The Pub => Off-topic => Topic started by: nend on May 1st, 2012, 07:36 PM

Title: HTML5 Upload Progress.
Post by: nend on May 1st, 2012, 07:36 PM
I haven't been playing much with HTML5 lately but found a new feature that I really like that I can't seem to get to work. The new feature is the ability to get the file upload progress without having the server tell the script how much it has got. It works off the browser and shows how much has been sent. But in my case it shows nothing, I can't get it to work. Anybody else play around with this new feature?

Source, modified to hide urls
Code: [Select]
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function sendForm(form) {
var fd = new FormData(form);
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.open("POST", form.action, true);
xhr.send(fd);
}
function uploadProgress(evt) {
var percentComplete = Math.round(evt.loaded * 100 / evt.total);
document.getElementById('prog').value = percentComplete;
}
</script>
</head>
<body>
<form action="http://somesite.com/?do=myRecs&sa=sub2" method="post" enctype="multipart/form-data" accept-charset="UTF-8">
<strong>Name:</strong><br /><input type="text" name="name" size="35" /><br />
<strong>File:</strong><br /><input type="file" name="upload" /><br />
<strong>File Name:</strong><br /><input type="text" name="file" size="35" /><br />
<strong>Version:</strong><br /><input type="text" name="ver" size="35" /><br />
<input type="submit" onclick="sendForm(this.form);" />
</form>
<progress id="prog" value="0" max="100.0"></progress>
</body>
</html>

There really isn't much information on it in the web, just that it is a new feature. There is not even a really decent demo script. So forth I built this on the information I gathered, but no go. -_-
Title: Re: HTML5 Upload Progress.
Post by: Arantor on May 1st, 2012, 07:59 PM
I know Dragooon actually implemented this into a plugin for Wedge, but I can't remember offhand how he actually did it.
Title: Re: HTML5 Upload Progress.
Post by: nend on May 1st, 2012, 08:47 PM
Figured it out, it was my button
Code: [Select]
<input type="submit" onclick="sendForm(this.form);" />

Changed to
Code: [Select]
<input type="button" value="submit" onclick="sendForm(this.form);" />
Title: Re: HTML5 Upload Progress.
Post by: live627 on May 1st, 2012, 10:16 PM
Quote
Anybody else play around with this new feature?
I have...

You just need to ensure that sendForm returns false to prevent the form from being submitted.
Title: Re: HTML5 Upload Progress.
Post by: nend on May 4th, 2012, 06:24 PM
I hate asking for help but I can't figure it out.

Small uploads work fine, big uploads don't.

What is going on when a big upload is uploaded it shows it being uploaded and once it is done the redirect works but no information is collected to add to the database or file is present on the server. The script returns no error either. I am stumped.

My source, I say if you can't post your source then it is not secure, lol.
Code: [Select]
<?php
if (!defined('romz'))
die('Hacking attempt...');

global 
$context;

$subActions = array(
'' => 'manageadd',
'sub' => 'manageadd2',
'edit' => 'manageedit',
'edit2' => 'manageedit2',
'del' => 'managedel'
);

if (isset(
$context['user']) && isset($subActions[$_REQUEST['sa']])) {
         
$context['html'].= $subActions[$_REQUEST['sa']]();
} else {
die('Hacking attempt...');
}

function 
manageadd() {

global $settings$context;

$context['canvas']['head'].= '
<script type="text/javascript">
function sendForm(form) {
if (form.upload.value != \'\') {
document.getElementById(\'progress\').style.display = \'\';
}
var fd = new FormData(form);
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.upload.addEventListener("load", uploadComplete, false);
xhr.open("POST", form.action  = ((/\?/).test(form.action) ? "&" : "?")   (new Date()).getTime(), true);
xhr.send(fd);
}
function uploadProgress(evt) {
var percentComplete = Math.round(evt.loaded * 100 / evt.total);
document.getElementById(\'prog\').value = percentComplete;
document.getElementById(\'percent\').innerHTML = percentComplete "%<br />" evt.loaded " of " evt.total;
}
function uploadComplete(evt) {
window.location = "'
.$settings['url'].'?do=myRoms&sa=files&id='.$_REQUEST['id'].'"
}
function uploadFailed(evt) {
document.getElementById(\'upstatus\').innerHTML = "There was an error attempting to upload the file.";
}
function uploadCanceled(evt) {
document.getElementById(\'upstatus\').innerHTML = "The upload has been canceled by the user or the browser dropped the connection.";
}
</script>
'
;

$request mdb_query("
SELECT *
FROM cat
WHERE romId = '"
.mysql_real_escape_string($_REQUEST['id'])."'
ORDER BY cat.name DESC
LIMIT 50"
);

$data.= '
<form action="'
.$settings['url'].'?do=manrel&sa=sub" method="post" enctype="multipart/form-data" accept-charset="UTF-8">
<strong>Category:</strong><br />
<select name="catId">'
;
while ($cat mysql_fetch_assoc($request)) {
$data.= '
<option value="'
.$cat['id'].'">'.$cat['name'].'</option>';
}
$data.= '
</select><br />
<strong>Name:</strong><br /><input type="text" name="name" size="35"  /><br />
<strong>Description:</strong><br /><textarea name="desc" rows="15" cols="35" ></textarea><br />
<strong>ChangeLog:</strong><br /><textarea name="chglog" rows="15" cols="35" ></textarea><br />
<strong>File Name:</strong> Filename to save file as.<br /><input type="text" name="file" size="35"  /><br />
<strong>Upload File:</strong> Upload a file, if linking then put the link in the URL field below. 110 MB\'s & 3 hours max.<br /><input type="file" name="upload" /><br />
<strong>Link to Url:</strong> Link to a file, if uploading leave this blank.<br /><input type="text" name="url" size="35"  /><br />
<strong>Version:</strong><br /><input type="text" name="ver" size="35"  /><br />
<strong>Release Type:</strong><br />
<select name="relType">
<option value="0">Full</option>
<option value="1">Hot Fix</option>
</select><br />
<strong>Sub Version:</strong> If this release is a hotfix please put in a sub version number.<br /><input type="text" name="subVer" size="35"  /><br />
<strong>MD5 Hash:</strong><br /><input type="text" name="md5" size="35"  /><br />
<strong>Set as Current Release:</strong> <input type="checkbox" name="verUpdate" value="true" checked /><br />
<input type="hidden" name="romId" value="'
.$_REQUEST['id'].'"  />
<input type="button" value="Submit" onclick="sendForm(this.form);" />
</form>
<div id="progress" style="display:none;background-color: rgb(0, 0, 0);opacity: 0.7;-moz-opacity:0.70;filter: alpha(opacity=70);z-index: 20;height: 100%;width: 100%;background-repeat:repeat;position:fixed;top: 0px;left: 0px;">
<div style="border-radius:4px;background-color:#FFFFFF;position:fixed;top:40%;left:40%;border:1px solid;width:250px;height:100px;text-align:center;padding-top:4px;">
<div id="upstatus">Uploading Please Wait</div>
<progress id="prog" value="0" max="100.0"></progress> <span id="percent"></span>
</div>
</div>'
;

return $data;
}

function 
manageadd2() {

global $context$settings;

// These files can be huge.
ini_set('upload_max_filesize''110M'); //110 MB's max
ini_set('post_max_size''120M'); // Which gives 10 MB's left for post
ini_set('max_input_time'10800); // 3 hours
ini_set('max_execution_time'10800);
ini_set('memory_limit''128M');

// Check file
if (!empty($_FILES['upload'])) {
$md5 hash_file('md5'$_FILES['upload']['tmp_name']);
if(isset($_REQUEST['md5']) && $_REQUEST['md5'] != '' && $md5 != $_REQUEST['md5']) {
$context['error'] = '<b>Hash Mismatch.</b><br />Your hash - '.$_REQUEST['md5'].'<br />Generated Hash - '.$md5;
return null;
}
$_REQUEST['md5'] = $md5;
}

$stamp date('Ymd');

mdb_insert(
'roms', array(
'devId' => $context['user']['id'],
'romId' => $_REQUEST['romId'],
'catId' => $_REQUEST['catId'],
'name' => $_REQUEST['name'],
'desc' => $_REQUEST['desc'],
'chglog' => $_REQUEST['chglog'],
'file' => $_REQUEST['file'],
'url' => $_REQUEST['url'],
'ver' => $_REQUEST['ver'],
'subVer' => $_REQUEST['subVer'],
'relType' => $_REQUEST['relType'],
'md5' => $_REQUEST['md5'],
'date' => $stamp
)
);

$newId mysql_insert_id();
if (isset($_REQUEST['verUpdate']) && $_REQUEST['verUpdate'] == 'true') {
mdb_query("UPDATE `rom` SET `verId` = '".mysql_real_escape_string($newId)."',`ver` = '".mysql_real_escape_string($_REQUEST['ver'])."',`subVer` = '".mysql_real_escape_string($_REQUEST['subVer'])."',`date` = '".$stamp."' WHERE `rom`.`id`=".mysql_real_escape_string($_REQUEST['romId'])." LIMIT 1");
mdb_query("UPDATE `cat` SET `verId` = '".mysql_real_escape_string($newId)."',`ver` = '".mysql_real_escape_string($_REQUEST['ver'])."',`subVer` = '".mysql_real_escape_string($_REQUEST['subVer'])."',`date` = '".$stamp."' WHERE `cat`.`id`=".mysql_real_escape_string($_REQUEST['catId'])." LIMIT 1");
}

if (!empty($_FILES['upload'])) { //New JS for upload cuasing problems, seems it leaves uploads always set even if there isn't any, will fix later.
move_uploaded_file ($_FILES['upload']['tmp_name'], $settings['path'].'/f/rom/'.$newId.'.null');
if (file_exists($settings['path'].'/f/rom/'.$newId.'.null')) {mdb_query("UPDATE `roms` SET `url` = '".$settings['url']."f/rom/".$newId.".null' WHERE `roms`.`id`=".$newId." LIMIT 1");}
}

header'Location: '.$settings['url'].'?do=myRoms&sa=files&id='.$_REQUEST['romId']);
}

function 
manageedit() {

global $settings;

$request mdb_query("
SELECT *
FROM roms
WHERE id = '"
.mysql_real_escape_string($_REQUEST['id'])."'
LIMIT 1"
);

$rom mysql_fetch_assoc($request);

$request mdb_query("
SELECT *
FROM cat
WHERE romId = '"
.$rom['romId']."'
ORDER BY cat.name DESC
LIMIT 50"
);

$data.= '
<form action="'
.$settings['url'].'?do=manrel&sa=edit2" method="post" accept-charset="UTF-8">
<strong>Category:</strong><br />
<select name="catId">'
;
while ($cat mysql_fetch_assoc($request)) {
$data.= '
<option value="'
.$cat['id'].'"'.($cat['id'] == $rom['catId'] ? ' selected="selected"' '').'>'.$cat['name'].'</option>';
}
$data.= '
</select><br />
<strong>Name:</strong><br /><input type="text" name="name" size="35" value="'
.$rom['name'].'" /><br />
<strong>Description:</strong><br /><textarea name="desc" rows="15" cols="35" >'
.$rom['desc'].'</textarea><br />
<strong>ChangeLog:</strong><br /><textarea name="chglog" rows="15" cols="35" >'
.$rom['chglog'].'</textarea><br />
<strong>File:</strong> Filename to save file as.<br /><input type="text" name="file" size="35" value="'
.$rom['file'].'" /><br />
<strong>Url:</strong><br /><input type="text" name="url" size="35" value="'
.$rom['url'].'" /><br />
<strong>Version:</strong><br /><input type="text" name="ver" size="35" value="'
.$rom['ver'].'" /><br />
<strong>Release Type:</strong><br />
<select name="relType">
<option value="0"'
.($rom['relType'] == ' selected="selected"' '').'>Full</option>
<option value="1"'
.($rom['relType'] == ' selected="selected"' '').'>Hot Fix</option>
</select><br />
<strong>Sub Version:</strong> If this release is a hotfix please put in a sub version number.<br /><input type="text" name="subVer" value="'
.$rom['subVer'].'" size="35"  /><br />
<strong>MD5 Hash:</strong><br /><input type="text" name="md5" size="35" value="'
.$rom['md5'].'" /><br />
<strong>Update as Current Release:<strong> <input type="checkbox" name="verUpdate" value="true" /><br />
<input type="hidden" name="romId" value="'
.$rom['id'].'" />
<input type="hidden" name="redirect" value="'
.$rom['romId'].'" />
<input type="submit" value="Submit" />
</form>'
;

return $data;
}

function 
manageedit2() {
global $settings;
mdb_query("UPDATE `roms` SET `name` = '".mysql_real_escape_string($_REQUEST['name'])."',`catId` = '".mysql_real_escape_string($_REQUEST['catId'])."',`desc` = '".mysql_real_escape_string($_REQUEST['desc'])."',`chglog` = '".mysql_real_escape_string($_REQUEST['chglog'])."',`file` = '".mysql_real_escape_string($_REQUEST['file'])."',`url` = '".mysql_real_escape_string($_REQUEST['url'])."',`ver` = '".mysql_real_escape_string($_REQUEST['ver'])."',`subVer` = '".mysql_real_escape_string($_REQUEST['subVer'])."',`relType` = '".mysql_real_escape_string($_REQUEST['relType'])."',`md5` = '".mysql_real_escape_string($_REQUEST['md5'])."' WHERE `roms`.`id`=".mysql_real_escape_string($_REQUEST['romId'])." LIMIT 1");

$stamp date('Ymd');
if (isset($_REQUEST['verUpdate']) && $_REQUEST['verUpdate'] == 'true') {
mdb_query("UPDATE `rom` SET `verId` = '".mysql_real_escape_string($_REQUEST['romId'])."',`ver` = '".mysql_real_escape_string($_REQUEST['ver'])."',`subVer` = '".mysql_real_escape_string($_REQUEST['subVer'])."',`date` = '".$stamp."' WHERE `rom`.`id`=".mysql_real_escape_string($_REQUEST['redirect'])." LIMIT 1");
mdb_query("UPDATE `cat` SET `verId` = '".mysql_real_escape_string($_REQUEST['romId'])."',`ver` = '".mysql_real_escape_string($_REQUEST['ver'])."',`subVer` = '".mysql_real_escape_string($_REQUEST['subVer'])."',`date` = '".$stamp."' WHERE `cat`.`id`=".mysql_real_escape_string($_REQUEST['catId'])." LIMIT 1");
}

header'Location: '.$settings['url'].'?do=myRoms&sa=files&id='.$_REQUEST['redirect']);
}

function 
managedel() {

global $settings;

$request mdb_query("
SELECT *
FROM cat
WHERE verId = '"
.mysql_real_escape_string($_REQUEST['id'])."'
ORDER BY date DESC
LIMIT 1"
);

if (mysql_num_rows($request) == 0) {
mdb_query("DELETE FROM `roms` WHERE id = ".mysql_real_escape_string($_REQUEST['id']));
if (file_exists($settings['path'].'/f/rom/'.$_REQUEST['id'].'.null')) {unlink($settings['path'].'/f/rom/'.$_REQUEST['id'].'.null');}
header'Location: '.$settings['url'].'?do=myRoms');
} else {
global $context;
$context['error'] = '<b>Current Release.</b><br />This is the most current release, please add a newer release or promote a older release as current before deleting.';
return null;
}
}
?>

Sorry though about all the source, I just can't figure it out and can't ask anywhere else because there really isn't a place to ask where I am going to get a intelligent answer.

Function with the form is manageadd and manageadd2 saves the information
Title: Re: HTML5 Upload Progress.
Post by: Dragooon on May 4th, 2012, 07:05 PM
You can't alter upload_max_filesize or post_max_size from a php script, you need to use php.ini or .htaccess. So your script is probably dying because of upload restrictions.
Quote from Arantor on May 1st, 2012, 07:59 PM
I know Dragooon actually implemented this into a plugin for Wedge, but I can't remember offhand how he actually did it.
JavaScript!
Title: Re: HTML5 Upload Progress.
Post by: Arantor on May 4th, 2012, 07:51 PM
Yes, I knew it was JS but I didn't know how it actually worked :P
Title: Re: HTML5 Upload Progress.
Post by: nend on May 4th, 2012, 08:49 PM
Still not taking, contacting support. :whistle:

Max upload size limit I am allowed to set in PHP is 192MB according to the hosting documentation. However no matter what I put in the PHP.ini it isn't taking even after rebooting my web services. I think it was working until I opted in to upgrade to PHP 5.3.

So all my sites with custom PHP.ini files are affected by the PHP upgrade. This turns out to be true because a coppermine site of mine which I increased the limits in PHP.ini is now not allowing big files, which it did before.

Got to love these new grid systems, there are side affects running a grid though. The most pain in the @#$% is that allot of times new files are not available to all nodes instantly.