File Upload using AngularJS

前端 未结 29 2038
野趣味
野趣味 2020-11-21 07:24

Here is my HTML form:

29条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 08:20

    The code will helps to insert file

    
    
        

    Select Picture

    insert.js

    var app = angular.module('myApp',[]);
    app.service('uploadFile', ['$http','$window', function ($http,$window) {
        this.uploadFiletoServer = function(file,uploadUrl){
            var fd = new FormData();
            fd.append('file', file);
            $http.post(uploadUrl, fd, {
                transformRequest: angular.identity,
                headers: {'Content-Type': undefined}
            })
            .success(function(data){
                alert("insert successfull");
                $window.location.href = ' ';//your window location
            })
            .error(function(){
                alert("Error");
            });
        }
    }]);
    app.controller('insert_Ctrl',  ['$scope', 'uploadFile', function($scope, uploadFile){
        $scope.uploadFile = function() {
            $scope.myFile = $scope.files[0];
            var file = $scope.myFile;
            var url = "save_data.php";
            uploadFile.uploadFiletoServer(file,url);
        };
        $scope.uploadedFile = function(element) {
            var reader = new FileReader();
            reader.onload = function(event) {
                $scope.$apply(function($scope) {
                    $scope.files = element.files;
                    $scope.src = event.target.result  
                });
            }
            reader.readAsDataURL(element.files[0]);
        }
    }]);
    

    save_data.php

    
    

提交回复
热议问题