【问题标题】:Hello how to upload an image using angularjs+mysql+node.js您好如何使用 angularjs+mysql+node.js 上传图片
【发布时间】:2016-03-17 03:40:10
【问题描述】:

angularjs和mysql和node.js如何上传图片?

<html>

   <head>
      <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
   </head>

   <body ng-app = "myApp">

      <div ng-controller = "myCtrl">
         <input type = "file" file-model = "myFile"/>
         <button ng-click = "uploadFile()">upload me</button>
      </div>

      <script>
         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 ' );
               console.dir(file);

               var uploadUrl = "/fileUpload";
               fileUpload.uploadFileToUrl(file, uploadUrl);
            };
         }]);

      </script>

   </body>
</html>

【问题讨论】:

  • 您是否遇到任何错误或此代码的实际问题是什么。
  • 您是否尝试将图像保存到数据库?将文件复制到服务器并将新路径保存到数据库中的图像更容易。
  • 是的,我的兄弟.....我正在使用 angularjs 和 node.js 和 mysql .......当我上传时没有保存在 db 中的路径

标签: mysql angularjs node.js


【解决方案1】:

AngularJs 是一种客户端语言。所以无法直接将数据保存到MySql。

如果任何数据存储在数据库或服务器中,您必须使用任何服务器端语言。

您正在创建一个可以跟踪来自客户端 (AngularJS) 的响应并发送正确响应的 Web 服务。

使用 NodeJs 在 Mysql 中上传文件

fs.open(temp_path, 'r', function (status, fd) {
    if (status) {
        console.log(status.message);
        return;
    }
    var buffer = new Buffer(getFilesizeInBytes(temp_path));
    fs.read(fd, buffer, 0, 100, 0, function (err, num) {
    var query = "INSERT INTO `files` SET ?",
    values = {
               file_type: 'img',
               file_size: buffer.length,
               file: buffer
     };
    mySQLconnection.query(query, values, function (er, da) {
      if (er)throw er;
    });
  });
});

function getFilesizeInBytes(filename) {
 var stats = fs.statSync(filename)
 var fileSizeInBytes = stats["size"]
 return fileSizeInBytes
}

【讨论】:

  • 兄弟,我正在使用 node.js 作为服务器端语言...但没有将路径保存在 mysql 的 db 中...所以您能帮我发送编码吗??跨度>
  • 如何使用 RestAPI(node js) 将文件从客户端(angular js)上传到文件夹。并将其图像位置路径保存在数据库中。
猜你喜欢
  • 2018-12-17
  • 2018-06-04
  • 2022-01-15
  • 2016-07-24
  • 2015-10-02
  • 2019-05-19
  • 1970-01-01
  • 1970-01-01
  • 2014-07-11
相关资源
最近更新 更多