File Upload using AngularJS

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

Here is my HTML form:

29条回答
  •  情歌与酒
    2020-11-21 08:24

    this works

    file.html

    
       
          
       
       
          

    controller.js

         var app = angular.module('app', []);
    
         app.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(res){
                    console.log(res);
               }).error(function(error){
                    console.log(error);
               });
            }
         }]);
    
         app.controller('fileCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){
            $scope.uploadFile = function(){
               var file = $scope.myFile;
    
               console.log('file is ' );
               console.dir(file);
    
               var uploadUrl = "/fileUpload.php";  // upload url stands for api endpoint to handle upload to directory
               fileUpload.uploadFileToUrl(file, uploadUrl);
            };
         }]);
    
      
    

    fileupload.php

      
    

提交回复
热议问题