0

enter image description hereI'm getting an issue of 'document not found' in nodejs using with ejs. I want to read the file from client using "input type= file" and get the data of file to my server.

/=======Nodejs Code:======/

 router.get('/upload',(req,res)=>{

      const fileData= document.getElementById('uploadForm').files[0].path;
              fs.readFile(fileData,(err,data)=>{
                  if(err) throw err;
                 // let dataObject =JSON.parse(data);
                  console.log(data);
              });
       });

/======Ejs Code:======/

<form  action="/upload" method="POST">
                        <div class="col-md-6">
                              Upload License File (XLS / TXT): <input type="file"  id="uploadForm" name="uploadForm" >
                              <button type="submit"  class="btn btn-primary" style="margin-top: 10px;border-radius:0;">Upload</button>

                        </div>
                  </form>

  <script type="text/javascript" src="/routes/index.js"> 
                                          var x = document.getElementById('uploadForm'); //make sure this tag is below "demo"
                                    </script>  




ReferenceError: document is not defined
    at Object.<anonymous> (C:\Users\Pranay\Desktop\nodejs_Lic\routes\index.js:101:14)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (C:\Users\Pranay\Desktop\nodejs_Lic\app.js:7:19)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
  • 1
    You forgot the `=` sign after your constant `fileData`. – rkg Sep 26 '19 at 07:08
  • 3
    You defined it in the backend. There is no `document` in nodejs. – tom Sep 26 '19 at 07:12
  • how do we read data from client to node js? – Pranay Chandale Sep 26 '19 at 07:16
  • 2
    `document` is most times a property of the `window` global object in the browser. Node.js doesn't run in the browser and it doesn't run in the context of a DOM content, so there is "document" to work with both because there is no `window` and because conceptually it doesn't make sense. Node as a backend can serve any document or even content it wishes, it needs not even be DOM, while code running in the browser is within the context of the current page that is loaded. – VLAZ Sep 26 '19 at 07:16
  • 1
    The form uses method POST, so the router should listen to POST, not GET: `router.post(...`. – Mehdi Sep 26 '19 at 07:30
  • 1
    The form data should be available in `req.body` at node.js side. Read also: https://stackoverflow.com/questions/9304888/how-to-get-data-passed-from-a-form-in-express-node-js – Mehdi Sep 26 '19 at 07:31

1 Answers1

0

Your code doesn't have a problem, you just forgot an = sign after your constant fileData.

rkg
  • 805
  • 5
  • 14