7

我想在我的页面上有一个按钮,我可以从该按钮从本地系统上传图像,然后我想将该图像保存在我的本地存储中。

我很想在这里学习 angularjs。

4

2 回答 2

9

您希望将图像编码为 base 64 字符串并将其存储在本地存储中。

有关如何将图像转换为 base 64 字符串的示例,请参阅此答案。toDataURL()返回一个字符串,然后您可以像通常将字符串存储在 JSON 对象中一样存储该字符串。

要显示图像,您可以使用以下内容:

<img src="data:image/jpeg;base64,blahblahblah"></img>

blahblahblah返回的字符串在哪里。

于 2013-08-31T10:37:06.390 回答
1

按照以下代码使用 AngularJS 上传和保存图像

创建index.php文件并初始化应用程序并创建 AngularJS 控制器。

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular-route.min.js"></script>
        <script src="app.js"></script>
    </head>
    <body ng-app="myApp" ng-controller="myCtrl">
        <div>
            <input type="file" file-model="myFile"/>
            <button ng-click="uploadFile()">upload me</button>
        </div>
    </body>
 </html>

在此之后,创建app.js并编写代码以使用 AngularJS 上传图像。

var myApp = angular.module('myApp', []);

myApp.directive('fileModel', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var model = $parse(attrs.fileModel);
            var modelSetter = model.assign;

            element.bind('change', function(){
                scope.$apply(function(){
                    modelSetter(scope, element[0].files[0]);
                });
            });
        }
    };
}]);

myApp.service('fileUpload', ['$http', function ($http) {
    this.uploadFileToUrl = function(file, uploadUrl){
        var fd = new FormData();
        fd.append('file', file);
        $http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        })
        .success(function(){
        })
        .error(function(){
        });
    }
}]);

myApp.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){

    $scope.uploadFile = function(){ 
        var file = $scope.myFile;
        console.log('file is ' + JSON.stringify(file));
        var uploadUrl = "post.php";
        fileUpload.uploadFileToUrl(file, uploadUrl);
    };

}]);

在此之后,创建post.php文件以将文件上传到存储中。

<?php $upload_dir = "images/"; 
if(isset($_FILES["file"]["type"]))
{ 
    $validextensions = array("jpeg", "jpg", "png", "gif");
    $temporary = explode(".", $_FILES["file"]["name"]);
    $file_extension = end($temporary);
    if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg")) && in_array($file_extension, $validextensions)) {
        if ($_FILES["file"]["error"] > 0){
            echo "Return Code: " . $_FILES["file"]["error"] . "<br/><br/>";
        } else {
            if (file_exists($upload_dir.$_FILES["file"]["name"])) {                
                echo 'File already exist';
            } else {
                $sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable
                $filename = rand().$_FILES['file']['name'];
                $targetPath = $upload_dir.$filename; // Target path where file is to be stored
                move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file
                echo 'success';
            }
        }
    } 
} ?>

创建图像文件夹。希望这会帮助你。供参考:http: //jsfiddle.net/JeJenny/ZG9re/

于 2015-05-22T07:34:03.390 回答