Skip to content
This repository has been archived by the owner on Mar 5, 2020. It is now read-only.

Commit

Permalink
Scriptaculous plugin to upload a file in ajax
Browse files Browse the repository at this point in the history
  • Loading branch information
jaymoulin committed Jun 5, 2016
1 parent c5425b9 commit b211754
Show file tree
Hide file tree
Showing 15 changed files with 8,783 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ajax/fileupload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
if (isset($_FILES['file']))
{
var_dump($_FILES);
var_dump($_POST);
}
?>
19 changes: 19 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<html>
<head>
<script type='text/javascript' src='js/prototype.js'></script>
<script type='text/javascript' src='js/scriptaculous.js'></script>
<script type='text/javascript' src='js/ajax.fileupload.js'></script>
<style type='text/css'>
#ajaxfileupload_infos {
background-color:#FF0000;
}
</style>
</head>

<body>
<div id='fileupload'></div>
</body>
<script type='text/javascript'>
test = new Ajax.FileUpload('fileupload', 'ajax/fileupload.php');
</script>
</html>
130 changes: 130 additions & 0 deletions js/Clean Firefox ajax.fileupload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* options: onComplete : function called when file upload is finished
onStart : function called when file upload start
multiple : bool that add a new ajax fileupload when submitting a file
loadingText : text shown when uploading a file (<name> shows the filename)
finishText : text shown when uploading a file (<name> shows the filename)
== Aditional informations ==
you can access the id of the instance with this.id in the callback functions
*/
Ajax.FileUpload = Class.create();
Ajax.FileUpload.prototype =
{
initialize: function(element, url, options) {
this.url = url;
this.element = element = $(element);

this.options = {
loadingText: 'Uploading <name>',
finishText: '<name> uploaded !',
multiple:true
}
if (options)
this.options = Object.extend(this.options, options);
this.createInstance();
},
createInstance: function() {
this.id = Math.ceil(Math.random() * 99999);
this.options.id = this.id;
this.createInfo();
this.createMyIframe();
this.createMyForm();
return (this.id);
},
createInfo: function () {
if (typeof(this.info) == "undefined")
{
this.info = document.createElement('div');
this.info.id = "ajaxfileupload_infos";
this.updateInfo("");
this.element.appendChild(this.info);
}
},
createMyIframe: function () {
if (typeof(this.iframe) == "undefined")
{
this.span = document.createElement('span');
this.iframe = document.createElement('iframe');
this.iframe.id = 'ajaxfileupload_iframe_'+this.id;
this.iframe.src = 'about:blank';
this.iframe.name = 'ajaxfileupload_iframe_'+this.id;
this.iframe.style.display = 'none';
this.iframe.appendChild(this.span);
this.element.appendChild(this.iframe);
}
},
createMyForm: function () {
if (typeof(this.form) == "undefined")
{
this.form = document.createElement('form');
this.form.method = 'POST';
this.form.action = this.url;
this.form.target = 'ajaxfileupload_iframe_'+this.id;
this.form.id = 'ajaxfileupload_form_'+this.id;
this.form.className = "ajaxfileupload_form";
this.form.enctype = 'multipart/form-data';

this.file = document.createElement('input');
this.file.type='file';
this.file.name='file';
this.file.className='ajaxfileupload_file';
this.file.id = 'ajaxfileupload_file_'+this.id;
this.setOnStart();
this.form.appendChild(this.file);
this.element.appendChild(this.form);
}
},
setOnStart: function () {
this.file.onchange = (function ()
{
this.onStart();
this.setOnComplete();
}).bind(this);
},
setOnComplete: function() {
this.iframe.onload = (function () {
this.onComplete();
if (typeof(this.options.onComplete) == "function")
this.options.onComplete();
}).bind(this);
},
getFilename: function() {
var b = this.file.value.replace(/^.*[\/\\]/g, '');
return b;
},
updateInfo: function (inbody) {
if (typeof(this.myinfo) == "undefined")
{
this.myinfo = document.createElement('div');
this.myinfo.id = 'ajaxfileupload_div_'+this.id;
this.myinfo.className = "ajaxfileupload_div";
this.myinfo.style.display = "none";
this.info.appendChild(this.myinfo);
}
else
{
this.myinfo.style.display = "block";
text = inbody.replace("<name>", this.getFilename());
this.myinfo.innerHTML = text;
}
},

onStart: function () {
this.form.style.display = 'none';
if (typeof(this.options.onStart) == "function")
this.options.onStart();
this.form.submit();
this.form.style.display = 'none';
this.updateInfo(this.options.loadingText);
if (this.options.multiple == true)
{
new Ajax.FileUpload(this.element, this.url, this.options);
}
},

onComplete: function () {
this.updateInfo(this.options.finishText)
}
}
163 changes: 163 additions & 0 deletions js/Save of ajax.fileupload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/**
* options: onComplete : function called when file upload is finished
onStart : function called when file upload start
multiple : bool that add a new ajax fileupload when submitting a file
loadingText : text shown when uploading a file (%name% shows the filename)
finishText : text shown when uploading a file (%name% shows the filename)
*/
Ajax.FileUpload = Class.create();
Ajax.FileUpload.options = Array();
Ajax.FileUpload.prototype =
{
initialize: function(element, url, options) {
console.debug('initialize');
this.url = url;
this.element = element = $(element);
this.options = Ajax.FileUpload.defaultOptions;
if (options)
this.options = Object.extend(this.options, options);
this.createInstance();
},
createInstance: function() {
this.id = Math.ceil(Math.random() * 99999);
console.debug('createInstance #'+this.id);
Ajax.FileUpload.options[this.id] = this.options;

this.createInfo();
this.createMyIframe();
this.createMyForm();
Ajax.FileUpload.updateInfo(this.id);
return (this.id);
},
createInfo: function () {
if ($('ajaxfileupload_infos') == null)
{
console.debug('info created #'+this.id);
myinfo = document.createElement('div');
myinfo.id = "ajaxfileupload_infos";
this.element.appendChild(myinfo);
}
else
{
console.debug('info getted #'+this.id);
myinfo = $('ajaxfileupload_infos');
}
this.info = myinfo;
},
createMyIframe: function () {
if ($('ajaxfileupload_iframe_'+this.id) == null)
{
myspan = document.createElement('span');
myiframe = document.createElement('iframe');
myiframe.id = 'ajaxfileupload_iframe_'+this.id;
myiframe.src = 'about:blank';
myiframe.name = 'ajaxfileupload_iframe_'+this.id;
myiframe.style.display = 'none';
myiframe.appendChild(myspan);
this.element.appendChild(myiframe);
}
},
createMyForm: function () {
if ($('ajaxfileupload_form_'+this.id) == null)
{
myform = document.createElement('form');
myform.method = 'POST';
myform.action = this.url;
myform.target = 'ajaxfileupload_iframe_'+this.id;
myform.id = 'ajaxfileupload_form_'+this.id;
myform.className = "ajaxfileupload_form";
myform.enctype = 'multipart/form-data';

myfile = document.createElement('input');
myfile.type='file';
myfile.name='file';
myfile.className='ajaxfileupload_file';
myfile.id = 'ajaxfileupload_file_'+this.id;
this.setOnStart(myfile);
myform.appendChild(myfile);
this.element.appendChild(myform);
}
},
setOnStart: function (myfile) {
console.debug('setOnStart #'+this.id);
myfile.onchange = function ()
{
var id = Ajax.FileUpload.getId(this.parentNode.id);
Ajax.FileUpload.onStart(id);
$('ajaxfileupload_iframe_'+id).onload = function () {
if (typeof(Ajax.FileUpload.options[id].onComplete) == "function")
Ajax.FileUpload.doFunction(Ajax.FileUpload.options[id].onComplete);
}
if (typeof(Ajax.FileUpload.options[id].onStart) == "function")
Ajax.FileUpload.doFunction(Ajax.FileUpload.options[id].onStart);
this.parentNode.submit();
this.parentNode.style.display = 'none';
Ajax.FileUpload.updateInfo(id, Ajax.FileUpload.defaultOptions.loadingText);
if (Ajax.FileUpload.options[id].multiple == true)
{
console.debug("found multiple upload");
this.createInstance();
}
}
}
}

Ajax.FileUpload.getId = function(id) {
var regexp = /^\w+_(\d+)$/;

if (found = regexp.exec(id)) {
return(Number(found[1]));
}
return(false);
}

Ajax.FileUpload.onStart = function(id) {
console.debug('onStart called #'+id);
$('ajaxfileupload_form_'+id).style.display = 'none';
$('ajaxfileupload_form_'+id)
}

Ajax.FileUpload.onComplete = function(id) {
console.debug('onComplete called #'+id);
}

Ajax.FileUpload.basename = function(path, suffix) {
var b = path.replace(/^.*[\/\\]/g, '');
if (typeof(suffix) == 'string' && b.substr(-suffix.length) == suffix) {
b = b.substr(0, b.length-suffix.length);
}
return b;
}

Ajax.FileUpload.doFunction = function (func) {
if (typeof(func) == "function")
func();
}

Ajax.FileUpload.updateInfo = function (id, body) {
if ($('ajaxfileupload_div_'+id) == null)
{
mydiv = document.createElement('div');
mydiv.id = 'ajaxfileupload_div_'+id;
mydiv.className = "ajaxfileupload_div";
mydiv.style.display = "none";
$('ajaxfileupload_infos').appendChild(mydiv);
}
else
{
mydiv = $('ajaxfileupload_div_'+id);
mydiv.style.display = "block";
text = body;
if ($('ajaxfileupload_file_'+id) != null)
{
text = body.replace("%name%", Ajax.FileUpload.basename($('ajaxfileupload_file_'+id).value));
}
mydiv.innerHTML = text;
}
}

Ajax.FileUpload.defaultOptions = {
loadingText: 'Uploading %name%',
finishText: '%name% uploaded !',
multiple:true
}
Loading

0 comments on commit b211754

Please sign in to comment.