如何使用Angular.js和PHP将图像保存到本地文件夹中

时间:2015-12-04 06:19:49

标签: javascript php jquery angularjs

我需要一个帮助。我需要使用Angular.js和PHP将图像存储在我的本地文件夹中。我正在解释下面的代码。

<div class="col-md-6 bannerimagefile" ng-controller="imgController">
    <form  name="form">
        <label for="bannerimage" accesskey="B">
            <span class="required">*</span> Banner Image
        </label>
        <input type="file" class="filestyle" data-size="lg" name="bannerimage" id="bannerimage" ngf-select ng-model="file" name="file" ngf-pattern="'image/*'" accept="image/*" ngf-max-size="20MB" ngf-min-height="100" ngf-resize="{width: 100, height: 100}" custom-on-change="uploadFile" multiple>
        <label for="bannerimage" accesskey="B" >
            <span class="required">*</span> View Image
        </label>
        <div style="padding-bottom:10px;" ng-repeat="step in stepsModel">
            <img ng-src="{{step}}" border="0" name="bannerimage" style="width:70px; height:70px;">
        </div>
        <div>
            <input type="button" id="btn" ng-click="submitImage();" value="Upload" />
        </div>
        <div class="clear"></div>
    </form>
</div>

控制器文件如下所示。

var app=angular.module('testImage',['ngFileUpload']);
app.controller('imgController',function($scope,$http){
    $scope.submitImage=function(){
        console.log('image',$scope.file);
    }
    $scope.stepsModel = [];
    $scope.uploadFile = function(event){
        console.log('event',event.target.files);
        var files = event.target.files;
        for (var i = 0; i < files.length; i++) {
            var file = files[i];
            var reader = new FileReader();
            reader.onload = $scope.imageIsLoaded; 
            reader.readAsDataURL(file);
        }
    };
    $scope.imageIsLoaded = function(e){
        $scope.$apply(function() {
            $scope.stepsModel.push(e.target.result);
        });
    }
});
app.directive('customOnChange', function() {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            var onChangeHandler = scope.$eval(attrs.customOnChange);
            element.bind('change', onChangeHandler);
        }
    };
});

这里我的要求是当用户点击上传按钮时,验证将检查并且图像将保存在项目文件夹中。我正在使用Angular.js和PHP。请帮我这样做。

1 个答案:

答案 0 :(得分:0)

如果您的问题是将图像存储在客户端(Angular侧) 我认为这应该有效 文件上传后,添加如下按钮或锚标记。

&LT; a href =“/ image-location-on-server / uploaded-image.jpg”download =“new-image-name.jpg”&gt;