在node.js中

时间:2015-12-18 05:14:13

标签: node.js

我想在工作区中上传文件,并在node.js中阅读。

Html代码:

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

</head>
<body>
<div ng-app="" ng-controller="SalesImportControl">
<form ng-submit="readCustomer()">

   <input type="file" name="file" file-model = "myFile">
    <br>
    <button type="submit" >Submit</button>
</form>
</div>
</body>
</html>

控制器:

'use strict';

angular.module('app')

.controller('SalesImportControl', ['$scope','$http', '$location', '$rootScope','fileUpload' , function($scope, $http, $location, $rootScope,fileUpload) {

    console.log(" In dataimportCtrl");



  //customer
        $scope.readCustomer = function(req,res){



               var file1=$scope.myFile;
               var fl1 = $scope.myFile.name;
               console.log("file name:"+$scope.myFile.name);


               var uploadUrl = "/fileUpload";

                var fd = new FormData();
               fd.append('file', file1);

        //File upload
              $http.post(uploadUrl, fd, {
                  transformRequest: angular.identity,
                  headers: {'Content-Type': undefined}
               }).success(function(data){


               }).error(function(){

               });


         //sales
            $http.post('/salesimport', { params: fl1  }).success(function(data) {


                console.log("in controller1");
                console.log("controller:"+data);

                $scope.data=data["jarr"];
              $scope.data1=data["jarr1"];
              $scope.data2=data["jarr2"];
              $scope.data3=data["jarr3"];
              $scope.data4=data["jarr4"];  


        }).error(function(response) {


         console.error("error in posting");


        });

    };

}])

Server.js:

var express = require('express');
var bodyparser = require('body-parser');
var mysql = require('mysql');
var app = express();

var formidable = require('formidable');
var path = require('path');

var fs = require('fs');
var _ = require('underscore');
var vouchersmodel = require('./models/v_db.js');

app.use(express.static(__dirname + "/public/angular"));
app.use(bodyparser.json());

app.listen(3000);
console.log('Server running on 3000');

app.post('/fileUpload', function (req, res) {

  var form = new formidable.IncomingForm();
    //Formidable uploads to operating systems tmp dir by default
    form.uploadDir = __dirname+"/models/upload2/";       //set upload directory
    form.keepExtensions = true;     //keep file extension

    form.parse(req, function(err, fields, files) {
        res.writeHead(200, {'content-type': 'text/xml'});
        res.write('received upload:\n\n');
        console.log("form.bytesReceived");

        console.log("file size: "+JSON.stringify(files.file.size));
        console.log("file path: "+JSON.stringify(files.file.path));
        console.log("file name: "+JSON.stringify(files.file.name));
        console.log("file type: "+JSON.stringify(files.file.type));
        console.log("astModifiedDate:"+JSON.stringify(files.file.lastModifiedDate));
        console.log("file:"+JSON.stringify(files.file));

    vouchersmodel.upload(files.file.path,files.file.name, function (msg) {
        return res.json(msg);
    });

    });

});

型号:

exports.upload= function(r,name,callback){

  var fs = require('fs');
var newPath = __dirname + "/upload2/"+name;
fs.readFile(r, function (err, data) {
fs.writeFile(newPath,data, function (err,res) {
   if(!err){

    console.log("uploaded.........");
    callback();

   }

         });

});

}

我的问题是它上传文件但在上传文件之前它在控制器中运行salesimport post方法并且没有显示这样的目录错误。 哪里错了?

注意: salesimport post方法请求访问模型中上传的文件。

0 个答案:

没有答案