Improve error reporting when file upload is not configured properly

This commit is contained in:
Frederic Guillot 2017-02-12 13:34:56 -05:00
parent a172e3ad8d
commit 991f7426e8
35 changed files with 129 additions and 46 deletions

View File

@ -3,6 +3,7 @@ Version 1.0.39 (unreleased)
Improvements:
* Improve error reporting when file upload is not configured properly
* Open comments in board view with a modal dialog instead of tooltip
* Improve card icons alignment on board
* Adjust modal dialog width on mobile devices

View File

@ -33,7 +33,7 @@ class AvatarFileController extends BaseController
$user = $this->getUser();
if (! $this->avatarFileModel->uploadImageFile($user['id'], $this->request->getFileInfo('avatar'))) {
$this->flash->failure(t('Unable to upload the file.'));
$this->flash->failure(t('Unable to upload files, check the permissions of your data folder.'));
}
$this->response->redirect($this->helper->url->to('AvatarFileController', 'show', array('user_id' => $user['id'])));

View File

@ -33,12 +33,21 @@ class ProjectFileController extends BaseController
public function save()
{
$project = $this->getProject();
$result = $this->projectFileModel->uploadFiles($project['id'], $this->request->getFileInfo('files'));
if (! $this->projectFileModel->uploadFiles($project['id'], $this->request->getFileInfo('files'))) {
$this->flash->failure(t('Unable to upload the file.'));
if ($this->request->isAjax()) {
if (! $result) {
$this->response->json(array('message' => t('Unable to upload files, check the permissions of your data folder.')), 500);
} else {
$this->response->json(array('message' => 'OK'));
}
} else {
if (! $result) {
$this->flash->failure(t('Unable to upload files, check the permissions of your data folder.'));
}
$this->response->redirect($this->helper->url->to('ProjectOverviewController', 'show', array('project_id' => $project['id'])), true);
}
$this->response->redirect($this->helper->url->to('ProjectOverviewController', 'show', array('project_id' => $project['id'])), true);
}
/**

View File

@ -52,12 +52,21 @@ class TaskFileController extends BaseController
public function save()
{
$task = $this->getTask();
$result = $this->taskFileModel->uploadFiles($task['id'], $this->request->getFileInfo('files'));
if (! $this->taskFileModel->uploadFiles($task['id'], $this->request->getFileInfo('files'))) {
$this->flash->failure(t('Unable to upload the file.'));
if ($this->request->isAjax()) {
if (! $result) {
$this->response->json(array('message' => t('Unable to upload files, check the permissions of your data folder.')), 500);
} else {
$this->response->json(array('message' => 'OK'));
}
} else {
if (! $result) {
$this->flash->failure(t('Unable to upload files, check the permissions of your data folder.'));
}
$this->response->redirect($this->helper->url->to('TaskViewController', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])), true);
}
$this->response->redirect($this->helper->url->to('TaskViewController', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])), true);
}
/**

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Nemoguće dodati pod-zadatak.',
'Sub-task added successfully.' => 'Pod-zadatak uspješno dodan.',
'Maximum size: ' => 'Maksimalna veličina: ',
'Unable to upload the file.' => 'Nije moguće snimiti fajl.',
'Display another project' => 'Prikaži drugi projekat',
'Created by %s' => 'Kreirao %s',
'Tasks Export' => 'Izvoz zadataka',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Nelze vytvořit dílčí úkol.',
'Sub-task added successfully.' => 'Dílčí úkol byl úspěšně přidán.',
'Maximum size: ' => 'Maximální velikost: ',
'Unable to upload the file.' => 'Soubor nelze nahrát.',
'Display another project' => 'Zobrazit jiný projekt',
'Created by %s' => 'Vytvořeno uživatelem %s',
'Tasks Export' => 'Export úkolů',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Under-opgaven kunne ikke oprettes.',
'Sub-task added successfully.' => 'Under-opgaven er tilføjet.',
'Maximum size: ' => 'Maksimum størrelse: ',
'Unable to upload the file.' => 'Filen kunne ikke uploades.',
'Display another project' => 'Vis et andet projekt...',
'Created by %s' => 'Oprettet af %s',
'Tasks Export' => 'Opgave eksport',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Erstellen der Teilaufgabe nicht möglich.',
'Sub-task added successfully.' => 'Teilaufgabe erfolgreich angelegt.',
'Maximum size: ' => 'Maximalgröße: ',
'Unable to upload the file.' => 'Hochladen der Datei nicht möglich.',
'Display another project' => 'Zu Projekt wechseln',
'Created by %s' => 'Erstellt durch %s',
'Tasks Export' => 'Aufgaben exportieren',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Αδύνατο να δημιουργηθεί η υπο-εργασία.',
'Sub-task added successfully.' => 'Η υπο-εργασία προστέθηκε με επιτυχία.',
'Maximum size: ' => 'Μέγιστο μέγεθος : ',
'Unable to upload the file.' => 'Δεν είναι δυνατή η μεταφόρτωση του αρχείου.',
'Display another project' => 'Εμφάνιση άλλου έργου',
'Created by %s' => 'Δημιουργήθηκε από %s',
'Tasks Export' => 'Εξαγωγή εργασιών',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'No se puede crear la subtarea.',
'Sub-task added successfully.' => 'Subtarea añadida correctamente.',
'Maximum size: ' => 'Tamaño máximo: ',
'Unable to upload the file.' => 'No se puede cargar el fichero.',
'Display another project' => 'Mostrar otro proyecto',
'Created by %s' => 'Creado por %s',
'Tasks Export' => 'Exportar tareas',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Alitehtävän luonti epäonnistui.',
'Sub-task added successfully.' => 'Alitehtävä luotiin onnistuneesti.',
'Maximum size: ' => 'Maksimikoko: ',
'Unable to upload the file.' => 'Tiedoston lataus epäonnistui.',
'Display another project' => 'Näytä toinen projekti',
'Created by %s' => 'Luonut: %s',
'Tasks Export' => 'Tehtävien vienti',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Impossible de créer votre sous-tâche.',
'Sub-task added successfully.' => 'Sous-tâche ajoutée avec succès.',
'Maximum size: ' => 'Taille maximum : ',
'Unable to upload the file.' => 'Impossible de transférer le fichier.',
'Display another project' => 'Afficher un autre projet',
'Created by %s' => 'Créé par %s',
'Tasks Export' => 'Exportation des tâches',
@ -1312,4 +1311,5 @@ return array(
'Do you really want to reopen this project: "%s"?' => 'Voulez-vous vraiment réouvrir ce projet : « %s » ?',
'This project is open' => 'Ce projet est ouvert',
'This project is closed' => 'Ce projet est fermé',
'Unable to upload files, check the permissions of your data folder.' => 'Impossible de transférer le ou les fichiers, vérifiez les permissions du répertoire de données.',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Részfeladat létrehozása nem lehetséges.',
'Sub-task added successfully.' => 'Részfeladat sikeresen létrehozva.',
'Maximum size: ' => 'Maximális méret: ',
'Unable to upload the file.' => 'Fájl feltöltése nem lehetséges.',
'Display another project' => 'Másik projekt megjelenítése',
'Created by %s' => 'Készítette: %s',
'Tasks Export' => 'Feladatok exportálása',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Tidak dapat membuat sub-tugas Anda.',
'Sub-task added successfully.' => 'Sub-tugas berhasil dibuat.',
'Maximum size: ' => 'Ukuran maksimum: ',
'Unable to upload the file.' => 'Tidak dapat mengunggah berkas.',
'Display another project' => 'Lihat proyek lain',
'Created by %s' => 'Dibuat oleh %s',
'Tasks Export' => 'Ekspor Tugas',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Impossibile creare il tuo sotto-task.',
'Sub-task added successfully.' => 'Sotto-task aggiunto con successo.',
'Maximum size: ' => 'Dimensioni massime: ',
'Unable to upload the file.' => 'Impossibile caricare il file.',
'Display another project' => 'Mostra un altro progetto',
'Created by %s' => 'Creato da %s',
'Tasks Export' => 'Export dei task',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'サブタスクの追加に失敗しました。',
'Sub-task added successfully.' => 'サブタスクを追加しました。',
'Maximum size: ' => '最大: ',
'Unable to upload the file.' => 'ファイルのアップロードに失敗しました。',
'Display another project' => '別のプロジェクトを表示',
'Created by %s' => '%s が作成',
'Tasks Export' => 'タスクの出力',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => '서브 할일의 추가에 실패했습니다.',
'Sub-task added successfully.' => '서브 할일을 추가했습니다.',
'Maximum size: ' => '최대: ',
'Unable to upload the file.' => '파일 업로드에 실패했습니다.',
'Display another project' => '프로젝트 보기',
'Created by %s' => '작성자 %s',
'Tasks Export' => '할일 내보내기',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Tidak dapat membuat sub-tugas anda.',
'Sub-task added successfully.' => 'Sub-tugas berhasil dibuat.',
'Maximum size: ' => 'Ukuran maksimum: ',
'Unable to upload the file.' => 'Tidak dapat mengunggah berkas.',
'Display another project' => 'Lihat projek lain',
'Created by %s' => 'Dibuat oleh %s',
'Tasks Export' => 'Ekspor Tugas',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Deloppgaven kunne ikke oprettes.',
'Sub-task added successfully.' => 'Deloppgaven er lagt til.',
'Maximum size: ' => 'Maksimum størrelse: ',
'Unable to upload the file.' => 'Filen kunne ikke lastes opp.',
'Display another project' => 'Vis annet prosjekt...',
'Created by %s' => 'Opprettet av %s',
'Tasks Export' => 'Oppgave eksport',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Subtaak aanmaken niet gelukt.',
'Sub-task added successfully.' => 'Subtaak succesvol aangemaakt.',
'Maximum size: ' => 'Maximale grootte : ',
'Unable to upload the file.' => 'Uploaden van bestand niet gelukt.',
'Display another project' => 'Een ander project weergeven',
'Created by %s' => 'Aangemaakt door %s',
'Tasks Export' => 'Taken exporteren',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Nie można utworzyć tego pod-zadania.',
'Sub-task added successfully.' => 'Pod-zadanie utworzone pomyślnie',
'Maximum size: ' => 'Maksymalny rozmiar: ',
'Unable to upload the file.' => 'Nie można wczytać pliku.',
'Display another project' => 'Wyświetl inny projekt',
'Created by %s' => 'Utworzone przez %s',
'Tasks Export' => 'Eksport zadań',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Não é possível criar a sua subtarefa.',
'Sub-task added successfully.' => 'Subtarefa adicionada com sucesso.',
'Maximum size: ' => 'Tamanho máximo: ',
'Unable to upload the file.' => 'Não foi possível carregar o arquivo.',
'Display another project' => 'Exibir outro projeto',
'Created by %s' => 'Criado por %s',
'Tasks Export' => 'Exportar Tarefas',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Não é possível criar a sua subtarefa.',
'Sub-task added successfully.' => 'Subtarefa adicionada com sucesso.',
'Maximum size: ' => 'Tamanho máximo: ',
'Unable to upload the file.' => 'Não foi possível carregar o arquivo.',
'Display another project' => 'Mostrar outro projeto',
'Created by %s' => 'Criado por %s',
'Tasks Export' => 'Exportar Tarefas',
@ -1312,4 +1311,5 @@ return array(
'Do you really want to reopen this project: "%s"?' => 'Deseja mesmo reabrir este projecto?: "%s"?',
'This project is open' => 'Este projeto está aberto',
'This project is closed' => 'Este projecto está fechado',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Не удалось создать подзадачу.',
'Sub-task added successfully.' => 'Подзадача добавлена.',
'Maximum size: ' => 'Максимальный размер: ',
'Unable to upload the file.' => 'Не удалось загрузить файл.',
'Display another project' => 'Показать другой проект',
'Created by %s' => 'Создано %s',
'Tasks Export' => 'Экспорт задач',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Nie można utworzyć tego pod-zadania.',
'Sub-task added successfully.' => 'Pod-zadatak utworzone pomyślnie',
'Maximum size: ' => 'Maksimalna veličina: ',
'Unable to upload the file.' => 'Nije moguće snimiti fajl.',
'Display another project' => 'Prikaži drugi projekat',
'Created by %s' => 'Kreirao %s',
'Tasks Export' => 'Izvoz zadataka',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Kunde inte skapa din deluppgift.',
'Sub-task added successfully.' => 'Deluppgiften har lagts till.',
'Maximum size: ' => 'Maxstorlek: ',
'Unable to upload the file.' => 'Kunde inte ladda upp filen.',
'Display another project' => 'Visa ett annat projekt',
'Created by %s' => 'Skapad av %s',
'Tasks Export' => 'Exportera uppgifter',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'ไม่สามารถสร้างงานย่อยได้',
'Sub-task added successfully.' => 'เพิ่มงานย่อยเรียบร้อยแล้ว',
'Maximum size: ' => 'ขนาดไฟล์สูงสุด:',
'Unable to upload the file.' => 'ไม่สามารถอัพโหลดไฟล์ได้',
'Display another project' => 'แสดงโปรเจคอื่น',
'Created by %s' => 'สร้างโดย %s',
'Tasks Export' => 'ส่งออกงาน',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => 'Alt görev oluşturulamadı.',
'Sub-task added successfully.' => 'Alt görev başarıyla eklendi.',
'Maximum size: ' => 'Maksimum boyutu',
'Unable to upload the file.' => 'Dosya yüklenemiyor.',
'Display another project' => 'Başka bir proje göster',
'Created by %s' => '%s tarafından oluşturuldu',
'Tasks Export' => 'Görevleri dışa aktar',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -282,7 +282,6 @@ return array(
'Unable to create your sub-task.' => '无法创建子任务',
'Sub-task added successfully.' => '成功添加子任务',
'Maximum size: ' => '大小上限:',
'Unable to upload the file.' => '无法上传文件',
'Display another project' => '显示其它项目',
'Created by %s' => '创建者:%s',
'Tasks Export' => '任务导出',
@ -1312,4 +1311,5 @@ return array(
// 'Do you really want to reopen this project: "%s"?' => '',
// 'This project is open' => '',
// 'This project is closed' => '',
// 'Unable to upload files, check the permissions of your data folder.' => '',
);

View File

@ -4,7 +4,6 @@ namespace Kanboard\ServiceProvider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Kanboard\Core\ObjectStorage\FileStorage;
use Kanboard\Core\Paginator;
use Kanboard\Core\Http\OAuth2;
use Kanboard\Core\Tool;
@ -178,10 +177,6 @@ class ClassProvider implements ServiceProviderInterface
return new HttpClient($c);
};
$container['objectStorage'] = function () {
return new FileStorage(FILES_DIR);
};
$container['cspRules'] = array(
'default-src' => "'self'",
'style-src' => "'self' 'unsafe-inline'",

View File

@ -0,0 +1,51 @@
<?php
namespace Kanboard\ServiceProvider;
use Kanboard\Core\ObjectStorage\FileStorage;
use LogicException;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
/**
* Class ObjectStorageProvider
*
* @package Kanboard\ServiceProvider
* @author Frederic Guillot
*/
class ObjectStorageProvider implements ServiceProviderInterface
{
public function register(Container $container)
{
$container['objectStorage'] = function () {
if (file_exists(FILES_DIR)) {
if (! is_writable(FILES_DIR)) {
$stat = stat(FILES_DIR);
throw new LogicException(sprintf(
'The folder to store uploaded files is not writeable by your webserver user (file=%s; mode=%o; uid=%d; gid=%d)',
FILES_DIR,
$stat['mode'],
$stat['uid'],
$stat['gid']
));
}
} elseif (! @mkdir(FILES_DIR)) {
$folder = dirname(FILES_DIR);
$stat = stat($folder);
throw new LogicException(sprintf(
'Unable to create folder to store uploaded files, check the permissions of the parent directory (file=%s; mode=%o; uid=%d; gid=%d)',
$folder,
$stat['mode'],
$stat['uid'],
$stat['gid']
));
}
return new FileStorage(FILES_DIR);
};
return $container;
}
}

View File

@ -53,4 +53,5 @@ $container->register(new Kanboard\ServiceProvider\JobProvider());
$container->register(new Kanboard\ServiceProvider\QueueProvider());
$container->register(new Kanboard\ServiceProvider\ApiProvider());
$container->register(new Kanboard\ServiceProvider\CommandProvider());
$container->register(new Kanboard\ServiceProvider\ObjectStorageProvider());
$container->register(new Kanboard\ServiceProvider\PluginProvider());

File diff suppressed because one or more lines are too long

View File

@ -19,11 +19,17 @@ KB.component('file-upload', function (containerElement, options) {
KB.find('#file-item-' + currentFileIndex).add(errorElement);
}
function onServerError(response) {
var errorElement = KB.dom('div').addClass('file-error').text(response.message).build();
KB.find('#file-item-' + currentFileIndex).add(errorElement);
KB.trigger('modal.stop');
}
function onComplete() {
currentFileIndex++;
if (currentFileIndex < files.length) {
KB.http.uploadFile(options.url, files[currentFileIndex], onProgress, onComplete, onError);
KB.http.uploadFile(options.url, files[currentFileIndex], onProgress, onComplete, onError, onServerError);
} else {
KB.trigger('modal.stop');
KB.trigger('modal.hide');
@ -81,7 +87,7 @@ KB.component('file-upload', function (containerElement, options) {
function uploadFiles() {
if (files.length > 0) {
KB.http.uploadFile(options.url, files[currentFileIndex], onProgress, onComplete, onError);
KB.http.uploadFile(options.url, files[currentFileIndex], onProgress, onComplete, onError, onServerError);
}
}

View File

@ -83,14 +83,25 @@ KB.http.postForm = function (url, formElement) {
return (new KB.http.request('POST', url, {}, formData)).execute();
};
KB.http.uploadFile = function (url, file, onProgress, onComplete, onError) {
KB.http.uploadFile = function (url, file, onProgress, onComplete, onError, onServerError) {
var fd = new FormData();
fd.append('files[]', file);
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', onProgress);
xhr.upload.addEventListener('load', onComplete);
xhr.upload.addEventListener('error', onError);
xhr.open('POST', url, true);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
onComplete();
} else if (typeof onServerError !== 'undefined') {
onServerError(JSON.parse(xhr.responseText));
}
}
};
xhr.send(fd);
};