0

I want to save data to a file in a local file system.

the action what i want to implement is when a button click happens the data should pass to the back end and save the data using fs.

i have a fs code which is working fine.

 var fs = require("fs");
 var path = "D:\\Temp\\Test.txt";
 var data = "Hello";

 fs.writeFile(path, data, function(error) {
 if (error) {
   console.error("write error:  " + error.message);
 } else {
   console.log("Successful Write to " + path);
 }
 });

then i used above fs code in my developing as below.

angular front end Controller

$scope.sendToBack = function ()
{
     var csvData = {
                   dat: str
                   };           

       Event.postDataToCepFs(csvData)
           .success(function () {
               $scope.status = 'Reading the selected file';

           })
           .error(function (error) {
              $scope.status = 'Unable to insert data: ' ;
           });


    return str;

   };

FrontEnd Service Layer

  postDataToCepFs : function(event) {
        return $http.post('/api/sendDataToCepFs', event);
        $window.location.reload();
    },

Backend Route

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

        var path = "D:\\Test.txt";        

          fs.writeFile(path, res.body.dat, function(error) {
               if (error) {
                 console.error("write error:  " + error.message);
               } else {
                 console.log("Successful Write to " + path);
               }
          });        
     });

The above is my approach.but it gives "Failed to load resource: the server responded with a status of 500 (Internal Server Error)"

can anyone help me this this?

Thanks

3
  • Any error in node.js's console? Commented Jun 16, 2017 at 11:08
  • 1
    What's the type of res.body.dat? Commented Jun 16, 2017 at 11:56
  • yea. i didn't specify the type of res.body.dat.now its fixed.Thanks @Omri Luzon Commented Jun 16, 2017 at 12:36

1 Answer 1

2

Are you using any middleware? Multer will work fine. Find below the links for multer documentation and sample.

https://www.npmjs.com/package/multer

http://lollyrock.com/articles/express4-file-upload/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @M.Navy! it's fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.