如何修复“文档未定义”

时间:2019-09-26 07:07:21

标签: javascript node.js ejs

enter image description here我在与ejs一起使用的nodejs中遇到“找不到文档”的问题。我想使用“输入类型=文件”从客户端读取文件,并将文件数据获取到我的服务器。

/ ======= Nodejs代码:====== /

 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代码:====== /

<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 个答案:

答案 0 :(得分:0)

您的代码没有问题,您只是在常量=之后忘记了fileData符号。