我将在此处提供的解决方案基于此处的帖子:
在 JavaScript 中从 base64 字符串创建 Blob
我遇到了类似的情况,但是图像使用 Base64 存储在服务器上。当加载网页并从数据库中检索图像时,必须将这些图像添加回flow.files
数组中。图像使用 Base64 字符串保存在数据库中。因此,在页面加载期间,对我来说唯一的方法是将 Base64 字符串转换为 Blob 并将文件添加回flow.files
数组。
这使流控制器能够在从数据库加载页面后正常运行。
以下是步骤:
添加指令load-photo
并将其添加到additional_image1
使用 jQuery 在文档就绪事件中从数据库加载 Base64 字符串的输入元素中。
添加指令以访问元素并$scope.loadPhoto
在准备加载照片的文档上调用范围函数。
在加载照片功能中,将 Base64 转换为 Blob 并将文件添加到流控中。
确保手动同步范围变量$scope.imageStringB64
和输入元素,因为没有按预期工作。这是因为 Angular 之外的 jQuery 代码正在从数据库中加载输入元素,而我发现它们不是动态绑定的。additional_image1
ng-model
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 的更多选项,请参阅此代码示例:
小提琴.jshell.ne