Apijs 6.9.6 / vanilla-js

Envois de fichier

Sommaire

  1. Présentation
  2. Traitement du fichier
  3. Informations complémentaires

Modifier les options

  • apijs.config
  • apijs.config.dialog
  • apijs.config.upload

Présentation

Gestion de l'envoi de fichier.
Le code source des fonctions de démo (myFuncX) est disponible dans le fichier demo.js.

Bien que la taille et l'extension soient vérifiées avant l'envoi du formulaire, il est important que le script qui réceptionne le fichier ou les fichiers revérifie ces données.

01) Dialogue d'upload avec un fichier
apijs.upload.sendFile(string title, string action, string input, number onemax, string exts, function callback, mixed args, string icon) : boolean

02) Dialogue d'upload avec plusieurs fichiers
apijs.upload.sendFiles(string title, string action, string input, number onemax, number allmax, string exts, function callback, mixed args, string icon) : boolean

Traitement du fichier

Voici une implémentation minimale du fichier qui traite le fichier avant de l'enregistrer sur le serveur.

php// stop output buffering with apache
// <Location ~ "this_file">
//     php_flag zlib.output_compression off
// </Location>
// stop output buffering with lighttpd
// $HTTP["url"] =~ "this_file" {
//     server.stream-response-body = 2
// }
// stop output buffering in the script (https://stackoverflow.com/a/25835968)
// the echo move the progress bar to 100%
header('Content-Encoding: chunked');
header('Content-Type: text/plain; charset=utf-8');
header('Cache-Control: no-cache, must-revalidate');
ini_set('output_buffering', (PHP_VERSION_ID < 80100) ? '0' : 0);
ini_set('implicit_flush', (PHP_VERSION_ID < 80100) ? '1' : 1);
ob_implicit_flush(true);
for ($i = 0; $i < ob_get_level(); $i++)
	ob_end_clean();
echo ' ';

// action
try {
	$tmpFile = tempnam(sys_get_temp_dir(), ''); // /tmp/anDY8jnU
	// $_FILES['myInputImage']['tmp_name'] if one file sent, else $_FILES['myInputImage']['tmp_name'][0]...
	move_uploaded_file($_FILES['myInputImage']['tmp_name'], $tmpFile);
	echo 'success-'.basename($tmpFile);	 // success-anDY8jnU
}
catch (Throwable $t) {
	echo $t->getMessage();
}

Lorsque le traitement se passe bien, le fichier qui traite le fichier doit renvoyer success-fileid.
Le fileid est transmis à la fonction callback de la manière suivante : callback(fileid, args).

Informations complémentaires

Pour le paramètre icon les classes CSS suivantes :
- peuvent être utilisées : notransition lock
- ne peuvent pas être utilisées : information confirmation options upload progress waiting photo video iframe ajax start ready end reduce mobile tiny fullscreen slideshow loading download print error warning

API

jsapijs.i18n.translate("word");              // return string
apijs.i18n.translateNode("word");          // return domelement
apijs.i18n.changeLang("fr");               // return boolean

apijs.dialog.dialogInformation(...);       // return boolean
apijs.dialog.dialogConfirmation(...);      // return boolean
apijs.dialog.dialogFormOptions(...);       // return boolean
apijs.dialog.dialogFormUpload(...);        // return boolean
apijs.dialog.dialogProgress(...);          // return boolean
apijs.dialog.dialogWaiting(...);           // return boolean
apijs.dialog.dialogPhoto(...);             // return boolean
apijs.dialog.dialogVideo(...);             // return boolean
apijs.dialog.dialogIframe(...);            // return boolean
apijs.dialog.dialogAjax(...);              // return boolean
apijs.dialog.actionClose();                // return void
apijs.dialog.add("string1", "string2");    // return dialog object
apijs.dialog.remove("string1", "string2"); // return dialog object
apijs.dialog.toggle("string1", "string2"); // return dialog object
apijs.dialog.has("string1", "string2");    // return boolean (true if one found)

apijs.upload.sendFile(...);                // return boolean
apijs.upload.sendFiles(...);               // return boolean

apijs.slideshow.show("slideshow.2.1");     // return boolean
apijs.slideshow.actionFirst();             // return boolean
apijs.slideshow.actionPrev();              // return boolean
apijs.slideshow.actionNext();              // return boolean
apijs.slideshow.actionLast();              // return boolean

// default config //////////////////////////////////////////
apijs.config.lang = "auto";
apijs.config.debug = false;

apijs.config.dialog.closeOnClick = false;
apijs.config.dialog.restrictNavigation = true;
apijs.config.dialog.player = true; // true|false|userFunction(videoTag, url)

apijs.config.slideshow.ids = "slideshow";
apijs.config.slideshow.anchor = true;

apijs.config.upload.tokenName = "X-CSRF-Token";
apijs.config.upload.tokenValue = null;

// events //////////////////////////////////////////////////
self.addEventListener("apijsbeforeload", function () {

	apijs.dialog.htmlContentOrig = apijs.dialog.htmlContent;
	apijs.dialog.htmlContent = function (title, text) {
		alert(title);
		this.htmlContentOrig(title, text);
	};

	if (!apijs.i18n.data.hasOwnProperty("xy")) apijs.i18n.data.xy = {};
	apijs.i18n.data.xy["xyz"] = "Test";

	apijs.config.debug = true;
});

self.addEventListener("apijsload", function () {
	console.log("ready");
});

self.addEventListener("apijsajaxresponse", function (ev) {
	console.log(ev.detail);
});