Last active
February 8, 2016 12:10
-
-
Save tyoshikawa1106/eb8b3a5f0c9eb48894de 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 MAX_FILE_SIZE = 1000000; // After Base64 Encoding, this is the max file size [Check! 1MB!] | |
var MAX_FILE_CNT = 2; // Upload File Count | |
var CHUNK_SIZE = 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 uploadFile = document.getElementById('attamentFile'); | |
// 添付ファイル件数の判定 | |
var isErrorFileCount = checkAttachmentFileCnt(uploadFile.files.length, MAX_FILE_CNT); | |
if (isErrorFileCount) { | |
$scope.isLocked = false; | |
return false; | |
} | |
// ファイルサイズの判定 | |
var isErrorFileSize = checkAttachmentFileSize(uploadFile, MAX_FILE_SIZE); | |
if (isErrorFileSize) { | |
$scope.isLocked = false; | |
return false; | |
} | |
// 取引先の登録処理を実行 | |
doSaveAccountByApex($scope.account, uploadFile); | |
}; | |
/** | |
* ファイル件数の判定 | |
*/ | |
function checkAttachmentFileCnt(attamentFileCnt, maxFileCnt) { | |
var isError = false; | |
if (!!attamentFileCnt == 0) { | |
$scope.errorMessages = 'ファイルを選択してください。'; | |
isError = true; | |
} else if (attamentFileCnt > maxFileCnt) { | |
$scope.errorMessages = '添付できるファイルは' + maxFileCnt + 'つまでです。'; | |
isError = true; | |
} | |
return isError; | |
} | |
/** | |
* ファイルサイズの判定 | |
*/ | |
function checkAttachmentFileSize(uploadFile, maxFileSize) { | |
var isError = false; | |
for (var i=0; i < uploadFile.files.length; i++) { | |
(function(file) { | |
if (file.size > maxFileSize) { | |
$scope.errorMessages = 'ファイルサイズは1MBまでです'; | |
isError = true; | |
} | |
})(uploadFile.files[i]); | |
} | |
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); | |
} | |
} else { | |
alert(event.message); | |
} | |
$scope.isLocked = false; | |
return false; | |
}); | |
} | |
/** | |
* ファイルの読み込み | |
*/ | |
function doFilesRead(accountId, uploadFile) { | |
console.log('-- doFilesRead START --'); | |
// [Promise]ファイルの読み込み | |
var promiseFileRead = fileRead(accountId, uploadFile); | |
promiseFileRead.then(function(result) { | |
console.log('File Read Done = ' + result.length); | |
// ファイルリードの結果をつかってRemoteActionで登録処理を実行 | |
var subPromises = []; | |
for (var i = 0; i < result.length; i++) { | |
console.log('File Name = ' + result[i].name); | |
var fileName = result[i].name; | |
var attachment = result[i].attachment; | |
// RemoteAction処理をPromiseに追加 | |
subPromises.push(doSaveAttachmentByApex(accountId, null, fileName, attachment, 0)); | |
} | |
// PromiseAll処理 | |
var subPromiseAll = $q.all(subPromises); | |
subPromiseAll.then(function() { | |
// 検証用メッセージ | |
console.log('-- Upload!!! --'); | |
$scope.errorMessages = '登録成功!' | |
}); | |
}); | |
} | |
/** | |
* ファイルの読み込み | |
*/ | |
function fileRead(accountId, uploadFile) { | |
console.log('-- fileRead START --'); | |
var deferred = $q.defer(); | |
var readFiles = []; | |
for (var i = 0; i < uploadFile.files.length; i++) { | |
(function(file) { | |
var fileReader = new FileReader(); | |
fileReader.onloadend = function(e) { | |
var attachment = window.btoa(this.result); | |
var fileInfo = { | |
name : file.name, | |
size : file.size, | |
attachment : attachment | |
}; | |
readFiles.push(fileInfo); | |
// ファイル全件読み込んだら処理終了 | |
if (i == uploadFile.files.length) { | |
console.log('Complate File Read! = ' + uploadFile.files.length); | |
deferred.resolve(readFiles); | |
} | |
} | |
// ファイルリード実行 | |
fileReader.readAsBinaryString(file); | |
})(uploadFile.files[i]); | |
} | |
return deferred.promise; | |
} | |
/** | |
* 添付ファイルの登録処理(Apex) | |
* 一度の通信量に上限があるため複数回に分けて実行 | |
*/ | |
function doSaveAttachmentByApex(accountId, attachmentId, attachmentName, attachment, positionIndex) { | |
console.log('-- doSaveAttachmentByApex START --'); | |
var deferred = $q.defer(); | |
// ファイルのBody情報取得 | |
var doneUploading = false; | |
var fileSize = attachment.length; | |
var attachmentBody = ""; | |
if(fileSize <= positionIndex + CHUNK_SIZE) { | |
attachmentBody = attachment.substring(positionIndex); | |
doneUploading = true; | |
} else { | |
attachmentBody = attachment.substring(positionIndex, positionIndex + CHUNK_SIZE); | |
} | |
// ApexのPromise | |
var apexPromises = remoteAction(accountId, attachmentId, attachmentName, attachmentBody, positionIndex, doneUploading); | |
apexPromises.then(function(result) { | |
console.log('-- Apex Promise End -- ' + result); | |
deferred.resolve(); | |
return false; | |
}); | |
return deferred.promise; | |
} | |
function remoteAction(accountId, attachmentId, attachmentName, attachmentBody, positionIndex, doneUploading) { | |
var deferred = $q.defer(); | |
// RemoteAction | |
AttachmentUploaderController.doSaveAttachment(accountId, attachmentId, attachmentName, attachmentBody, | |
function(result, event){ | |
if(event.status) { | |
console.log(event.status); | |
if (result.errorMessages.length > 0) { | |
$scope.errorMessages = result.errorMessages; | |
} else { | |
if(doneUploading == true) { | |
console.log('Attachment Upload Done = ' + result.recordId); | |
deferred.resolve(result.recordId); | |
return false; | |
console.log('resolve?'); | |
} else { | |
console.log('再実行'); | |
// 添付ファイル登録処理の再呼び出し(Bodyの続きをアップロード) | |
doSaveAttachmentByApex(accountId, result.recordId, attachmentName, attachment, positionIndex + CHUNK_SIZE); | |
return false; | |
console.log('NG?'); | |
} | |
} | |
} else { | |
$scope.errorMessages = event.message; | |
} | |
console.log('エラー'); | |
// エラー発生時 | |
deferred.reject(); | |
return false; | |
}, | |
{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