From 55ca7c1c4f4b98d957cc155ec648bf50946f553e Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 22 Apr 2014 11:21:58 +0300 Subject: [PATCH] upload attachment change --- application/Espo/Controllers/Attachment.php | 15 ++++++ .../src/views/fields/attachment-multiple.js | 50 +++++++++++-------- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/application/Espo/Controllers/Attachment.php b/application/Espo/Controllers/Attachment.php index 2641dd1b15..a2baabbc2f 100644 --- a/application/Espo/Controllers/Attachment.php +++ b/application/Espo/Controllers/Attachment.php @@ -25,4 +25,19 @@ namespace Espo\Controllers; class Attachment extends \Espo\Core\Controllers\Record { + public function actionUpload($params, $data) + { + list($prefix, $contents) = explode(',', $data); + $contents = base64_decode($contents); + + $attachment = $this->getEntityManager()->getEntity('Attachment'); + $this->getEntityManager()->saveEntity($attachment); + $this->getContainer()->get('fileManager')->putContents('data/upload/' . $attachment->id, $contents); + + return array( + 'attachmentId' => $attachment->id + ); + } + } + diff --git a/frontend/client/src/views/fields/attachment-multiple.js b/frontend/client/src/views/fields/attachment-multiple.js index 188a87b5df..e1cd7bc98d 100644 --- a/frontend/client/src/views/fields/attachment-multiple.js +++ b/frontend/client/src/views/fields/attachment-multiple.js @@ -169,35 +169,43 @@ Espo.define('Views.Fields.AttachmentMultiple', 'Views.Fields.Base', function (De } fileList.forEach(function (file) { - var attachment = model.clone(); var $att = this.addAttachmentBox(file.name); $att.find('.remove-attachment').on('click.uploading', function () { canceledList.push(attachment.cid); totalCount--; - }); - - attachment.set('name', file.name); - attachment.set('type', file.type || 'text/plain'); - attachment.set('size', file.size); - attachment.once('sync', function () { - if (canceledList.indexOf(attachment.cid) === -1) { - $att.trigger('ready'); - this.pushAttachment(attachment); - $att.attr('data-id', attachment.id); - uploadedCount++; - if (uploadedCount == totalCount) { - afterAttachmentsUploaded.call(this); - } - } - }, this); + }); + + var attachment = model.clone(); var fileReader = new FileReader(); - fileReader.onload = function (e) { - attachment.set('file', e.target.result); - attachment.save(); - }; + fileReader.onload = function (e) { + $.ajax({ + type: 'POST', + url: 'Attachment/action/upload', + data: e.target.result, + contentType: 'multipart/encrypted', + }).done(function (data) { + + attachment.id = data.attachmentId; + attachment.set('name', file.name); + attachment.set('type', file.type || 'text/plain'); + attachment.set('size', file.size); + attachment.once('sync', function () { + if (canceledList.indexOf(attachment.cid) === -1) { + $att.trigger('ready'); + this.pushAttachment(attachment); + $att.attr('data-id', attachment.id); + uploadedCount++; + if (uploadedCount == totalCount) { + afterAttachmentsUploaded.call(this); + } + } + }, this); + attachment.save(); + }.bind(this)); + }.bind(this); fileReader.readAsDataURL(file); }, this); }.bind(this));