上传Excel文件并使用Node js从mysql下载

时间:2018-01-23 10:12:29

标签: mysql angularjs node.js excel

我想上传Excel表格并在提交之后需要将数据插入mysql数据库和我们上传的同一张表需要下载。

我试过下面的代码: 节点服务 -

function getDetails(req, res) {
    var sampleFile, fileInfo = {};
          var post  = req.body;
          var ID= post.id;
          var name=post.name

        if (!req.files) {
            res.send('No files were uploaded.');
            return;
          }

          sampleFile = req.files.fileInputXLSX;
          console.log("req.body -- ",req.body);
          console.log("Uploaded -- ",sampleFile);
          // Get file attributes
          var fileId = req.body.fileId;
          var fileExtn = sampleFile.name.split(".").pop();
          var extractedFilename = sampleFile.name.slice(0, sampleFile.name.lastIndexOf('.'));
          var uploadFileName = extractedFilename+'_'+fileId+'.'+fileExtn;
          console.log("uploadFileName -- ",uploadFileName);
          fileInfo = {
            "name": uploadFileName,
            "mimetype": sampleFile.mimetype
          }
          sampleFile.mv(__dirname+'/Myuploads/Details/'+uploadFileName, function(err) {
            if (err) {
                res.status(500).send(err);

            }

            else {
                // Update file info
                var queryString = "INSERT INTO 'details'('id','name') VALUES ('" + ID + "','" + name + "')";
                     connection.query(queryString, function(err, result) {
                        if (!err){                  
                    var response = [];
                    response.push({'result' : 'success'});
                        if (result.length != 0) {
                            response.push({'data' : result});
                        } else {
                            response.push({'msg' : 'No Result Found'});
                        }

                        res.setHeader('Content-Type', 'application/json');
                        res.status(200).send(JSON.stringify(response));
                    }   else {
                        res.status(400).send(err);
                            }
});
            }
          });
    }

Controller.js

$scope.MyFunction=function(){
    var excelForm = new FormData();
        excelForm.append('fileInputXLSX', document.getElementById("fileInputXLSX").files[0]);
        console.log("---- excelFile : ", document.getElementById("fileInputXLSX").files[0]);
        // End : Get File


        $http.post(Data.baseNodeService + "getDetails", {
                "newProtocolObj": $scope.newProtocolObj
            },headconfig).success(function(data, status, headers, config) {
                console.log('Details: success');

                excelForm.append('fileId', data);
                jQuery.ajax({
                    url: data.baseNodeService + "getDetails",
                    type: "POST",
                    cache: false,
                    contentType: false,
                    processData: false,
                    data: excelForm,
                    success: function(data) {
                        console.log("---- upload response : ", data);
                        $scope.goToTfilePage();
                    }
                });
                // End : Upload File

            }).error(function(map_data, status, headers, config) {
                console.log('Details: error');
                console.log('status: ', status, '\nmap_data: ', map_data, '\nconfig: ', config);
            });


    }

消息来自控制台:没有上传文件。

请帮助相同。它不上传文件。它无法读取节点服务的响应。 我是这个帮助的新手,我需要以哪种方式写作。

编辑:我能够上传文件但是如何插入到mysql数据库中?

0 个答案:

没有答案