Created
February 3, 2016 07:20
-
-
Save tyoshikawa1106/43914b6552a7bed38952 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page > | |
<script type="text/javascript"> | |
(function(){ | |
beenFocused = true; | |
var maxStringSize = 6000000; //Maximum String size is 6,000,000 characters | |
var maxFileSize = 4350000; //After Base64 Encoding, this is the max file size | |
var chunkSize = 950000; //Maximum Javascript Remoting message size is 1,000,000 characters | |
var app = angular.module('myApp', ['ngMessages']); | |
// 共通変数(Account) | |
app.factory('Account', function() { | |
return { | |
Name : 'salesforce.com' | |
}; | |
}); | |
app.controller('mainCtrl', ["$scope", "$q" , 'Account', function($scope, $q, Account) { | |
$scope.account = Account; | |
$scope.isLocked = false; | |
$scope.errorMessages = []; | |
$scope.doSave = function(event) { | |
event.preventDefault(); | |
// 処理開始前にメッセージをクリア | |
$scope.errorMessages = []; | |
// 画面操作ロック | |
$scope.isLocked = true; | |
// ファイル情報の取得 | |
var attamentFile = document.getElementById('attamentFile'); | |
// 添付ファイル件数の判定 | |
var isErrorFileCount = checkAttachmentFileCnt(attamentFile.files.length); | |
if (isErrorFileCount) { | |
$scope.isLocked = false; | |
return false; | |
} | |
// 取引先の登録処理を実行 | |
doSaveAccountByApex($scope.account, attamentFile); | |
}; | |
/** | |
* ファイル件数の判定 | |
*/ | |
function checkAttachmentFileCnt(attamentFileCnt) { | |
var isError = false; | |
if (!!attamentFileCnt == 0) { | |
$scope.attamentFileCnt = 'ファイルを選択してください。'; | |
isError = true; | |
} else if (attamentFileCnt > 5) { | |
$scope.attamentFileCnt = '添付できるファイルは5つまでです。'; | |
isError = true; | |
} | |
return isError; | |
} | |
/** | |
* 取引先の登録処理(Apex) | |
* 完了後に添付ファイル登録処理を呼び出し | |
*/ | |
function doSaveAccountByApex(account, uploadFile) { | |
// RemoteAction | |
AttachmentUploaderController.doSaveAccount(account, function(result, event){ | |
if(event.status) { | |
if (result.errorMessages.length > 0) { | |
$scope.errorMessages = result.errorMessages; | |
} else { | |
// 添付ファイル登録処理の呼び出し | |
doFilesRead(result.recordId, uploadFile.files); | |
} | |
} else { | |
alert(event.message); | |
} | |
$scope.isLocked = false; | |
return false; | |
}); | |
} | |
/** | |
* ファイルの読み込み | |
*/ | |
function doFilesRead(accountId, files) { | |
// [Promise]ファイルの読み込み | |
var promiseA = fileRead(accountId, files); | |
var promiseB = promiseA.then(function(result) { | |
console.log('PromiseA'); | |
return result; | |
}); | |
var promiseC = promiseB.then(function(result) { | |
console.log('result.length = ' + result.length); | |
console.log(result); | |
$scope.record1 = result[0]; | |
$scope.record2 = result[1]; | |
// RemoteAction | |
AttachmentUploaderController.doSaveAttachment(accountId, null, $scope.record1.name, $scope.record1.attBody.substring(0), | |
function(result, event){ | |
if(event.status) { | |
if (result.errorMessages.length > 0) { | |
$scope.errorMessages = result.errorMessages; | |
} else { | |
console.log('OK!! = ' + result.recordId); | |
// RemoteAction | |
AttachmentUploaderController.doSaveAttachment(accountId, null, $scope.record2.name, $scope.record2.attBody.substring(0), | |
function(result, event){ | |
if(event.status) { | |
if (result.errorMessages.length > 0) { | |
$scope.errorMessages = result.errorMessages; | |
} else { | |
console.log('OK!! = ' + result.recordId); | |
} | |
} else { | |
alert(event.message); | |
} | |
}, | |
{buffer: true, escape: true, timeout: 120000} | |
); | |
} | |
} else { | |
alert(event.message); | |
} | |
}, | |
{buffer: true, escape: true, timeout: 120000} | |
); | |
return true; | |
}); | |
} | |
/** | |
* ファイルの読み込み | |
*/ | |
function fileRead(accountId, files) { | |
var deferred = $q.defer(); | |
var readFiles = []; | |
for (var i = 0; i < files.length; i++) { | |
(function(file) { | |
var fileReader = new FileReader(); | |
fileReader.onloadend = function(e) { | |
var attBody = window.btoa(this.result); | |
var fileInfo = { | |
name : file.name, | |
size : file.size, | |
attBody : attBody | |
}; | |
readFiles.push(fileInfo); | |
// ファイル全件読み込んだら処理終了 | |
if (i == files.length) { | |
console.log('File Read OK'); | |
deferred.resolve(readFiles); | |
} | |
} | |
// ファイルリード実行 | |
fileReader.readAsBinaryString(file); | |
})(files[i]); | |
} | |
return deferred.promise; | |
} | |
function doSaveAttachment(accountId, attachmentId, attachmentName, attachmentBody) { | |
var deferred = $q.defer(); | |
// RemoteAction | |
AttachmentUploaderController.doSaveAttachment(accountId, attachmentId, attachmentName, attachmentBody, | |
function(result, event){ | |
if(event.status) { | |
if (result.errorMessages.length > 0) { | |
$scope.errorMessages = result.errorMessages; | |
} else { | |
//if(doneUploading == true) { | |
console.log('OK!! = ' + result.recordId); | |
deferred.resolve(); | |
//} | |
} | |
} else { | |
alert(event.message); | |
} | |
deferred.reject(); | |
}, | |
{buffer: true, escape: true, timeout: 120000} | |
); | |
return deferred.promise; | |
} | |
}]); | |
})(); | |
</script> | |
</apex:page> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment