ここで提供するソリューションは、次の投稿に基づいています。
JavaScript で base64 文字列から Blob を作成する
同様のケースに直面しましたが、画像は Base64 を使用してサーバーに保存されます。Web ページが読み込まれ、画像がデータベースから取得されると、そのような画像をflow.files
配列に戻す必要があります。画像は Base64 文字列を使用してデータベースに保存されます。そのため、ページの読み込み中に、Base64 文字列を Blob に変換し、ファイルをflow.files
配列に戻すしか方法がありませんでした。
これにより、ページがデータベースからロードされた後、フロー コントローラーが正しく機能するようになりました。
手順は次のとおりです。
ディレクティブを追加し、 jQuery を使用してドキュメント準備完了イベントでデータベースから読み込まれた Base64 文字列を持つload-photo
入力要素に追加します。additional_image1
$scope.loadPhoto
ディレクティブを追加して要素にアクセスし、写真をロードする準備が整ったドキュメントでスコープ関数を呼び出します。
写真の読み込み機能で、Base64 を Blob に変換し、ファイルをフロー コントロールに追加します。
期待どおりに機能しなかったため、スコープ変数$scope.imageStringB64
と入力要素additional_image1
が手動で同期されていることを確認してください。ng-model
これは、angular 以外の jQuery コードがデータベースから入力要素を読み込んでおり、それらが動的にバインドされていないことがわかったためです。
JavaScript コード:
app.directive('loadPhoto', function () {
return function (scope, element, attrs) {
//This directive 'load-photo' is required to access the DOM element inside the scope
//This will get the Base64 string of the image which is stored in the input element
angular.element(document).ready(function () {
scope.loadPhoto(element[0]);
})
}
})
app.controller('photosController', ['$scope', '$http', '$timeout',
function ($scope, $http, $timeout) {
...
var BLANK_IMG_URL = "//:0";
$scope.removeFile = function () {
//debugger;
$scope.$flow.cancel();
$scope.imageStringB64 = '';
$scope.imageString = BLANK_IMG_URL;
}
$scope.loadPhoto = function (elem) {
//Load photo from Database
//The photo Base64 is stored in the input element 'elem'
var blobImage;
var tmpBase64;
tmpBase64 = angular.element(elem).val();
if (tmpBase64) {
//Convert Base64 to Blob object
blobImage = b64toBlob(tmpBase64, 'image/png');
blobImage.name = "image.png";
//Add the Blob object to flow files.
$scope.$flow.addFile(blobImage);
}
}
...
}]);
HTML コード:
<div class="photo-wrapper" ng-controller="photosController"
flow-init
flow-file-added="!isAppraiserSigned() && !!{png:1,gif:1,jpg:1,jpeg:1}[$file.getExtension()]"
flow-files-submitted="$flow.upload()">
<h4 class='photo-title'>Photo 1</h4>
<div class="property-photo drag-drop-photo" ng-hide="$flow.files.length" flow-drop
flow-drag-enter="isAppraiserSigned()?style={}:style={border:'4px solid green'}" flow-drag-leave="style={}" ng-style="style">
<div class='drag-drop-lbl'>Drop file here</div>
</div>
<div class="property-photo" flow-drop ng-show="$flow.files.length"
flow-drag-enter="isAppraiserSigned()?style={}:style={border:'4px solid green'}" flow-drag-leave="style={}" ng-style="style">
<img id="additional_image1_section" style="max-height:100%" flow-img="$flow.files[0]" />
<input id="additional_image1" name = "additional_image1" ng-hide="true" type="text" ng-model="imageStringB64" load-photo/>
</div>
<div>
<a href="#" class="btn" ng-hide="$flow.files.length" flow-btn flow-attrs="{accept:'image/*'}"><%=selectImageLbl%></a>
<a href="#" class="btn" ng-show="$flow.files.length" flow-btn flow-attrs="{accept:'image/*'}">Change</a>
<a href="#" class="btn btn-danger" ng-show="$flow.files.length"
ng-click="removeFile()">
Remove
</a>
</div>
<div class='photo-description'>
<label class='description-lbl' for='additional_image_describe1'><%=descriptionLbl%></label>
<input class='description' id='additional_image_describe1' name='additional_image_describe1' type="text" />
</div>
</div>
Base64 イメージを blob に変換してから Base64 に戻すその他のオプションについては、次のコード サンプルを参照してください。
fiddle.jshell.ne