【问题标题】:saving a form data as txt file or json to a server location将表单数据作为 txt 文件或 json 保存到服务器位置
【发布时间】:2019-08-11 06:17:45
【问题描述】:

我想通过输入相应的字段,从 UI 以 json 或文本文件格式在服务器位置提交表单数据作为键值对。我尝试了一些方法,但还没有运气。请问有什么建议吗?附上我的代码供您参考。`

function saveTalend() {
var location = document.getElementById('loc_st').value;
var sqlFileName = document.getElementById('txtfilePath').value;
var tableName = document.getElementById('dest_table').value;
var modeTal = document.getElementById('dest_mode').value;
var appName = e.name.value;

	var data = `input {
		application_name = "${appName}"
		location_talend = "${location}"
		hql_name = "${sqlFileName}"
		table_name = "${tableName}",
		mode = "${modeTal}"

	}`;
	e.nest_talend.value = tal_js.toString();
	e.action="/talend";
	return true;
}


app.post('/talend', function (req, res) {
    console.log("enetered talend")
    var app_name = '';

    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        res.writeHead(200, {
            'content-type': 'text/plain'
        });
        var oldpath = files.sqlfile.path;
        var base_path = 'C:\Projects';
        var newpath = '';
        app_name = fields.name.toString();
        if (/^win/i.test(process.platform)) {
            //windows is not perfectly yet but unix works without any issues
            //newpath = 'C://Users//atalap//' + files.sqlfile.name;
            newpath = process.env.USERPROFILE + '\\' + files.sqlfile.name;
            console.log("user Home path " + process.env.HOMEPATH + " HOME " + process.env.HOMEPATH + " USERPROFILE" + process.env.USERPROFILE);
        } else {
            newpath = base_path + app_name + '/hql/' + files.sqlfile.name;
        }
        var tal_js = fields.nest_talend.toString();
        console.log("hocon " + tal_js);
        // shell.mkdir('-p', base_path + appName + '/conf');
        shell.mkdir('-p', base_path + app_name + '/hql');
        // shell.rm('-rf', base_path + appName + '/hql/*');

        // var conf_file = base_path + appName + '/conf/' + appName + '.conf';

        fs.writeFileSync(tal_js, 'utf8', function (err) {
            if (err) {
                throw err;
            } else {
                console.log("form data written to " + tal_js);
            }
        });

        fs.rename(oldpath, newpath, function (err) {
            if (err) throw err;
        });

        console.log('last part of form.parse');
    });
    // res.redirect('./form.html');
    return;
});
<div class="form-group row">
            <label for="data" class="col-sm-2 col-form-label">Location</label>
            <div class="col-sm-2">
                <select class="form-control" id="loc_st" name="loc_st">
                    <option>Spark</option>
                    <option>Talend</option>
                </select>
            </div>
        </div>

<div class="container" id="myHeader">
                <div class="form-group col childContainer">

                    <label for="fileToLoad0" class="btn button1 btn-md btn-secondary">Import</label>
                  
                   <input type="file" id="fileToLoad0" name="sqlfile" style="display:none" onchange='openFile(event)'>
                    <input type="text" class="form-control" readonly="true" id="txtfilePath" style="width: 30%; margin-top: 15px; margin-left: 15px;" />


                    <div class="form-group col-lg-8 rep">
                        <hr />
                        <textarea id="txtSql" class="form-control clearBox fileContentBox" name="inputTextToSave" rows="5"
                            onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;">Enter HQL here..</textarea>
                        <br />
                      <div class="form-group col-md-3">
                                <label for="table">Table</label>
                                <input type="text" name="table" class="form-control clearBox" id="inputName1" id="table"
                                    placeholder="Table Name">
                            </div>

                            <div class="form-group col-md-3">
                                <label for="mode">Mode</label>
                                <select class="form-control" id="inputName2">
                                    <option>Overwrite</option>
                                    <option>Append</option>
                                </select>
                            </div>
                        </div>
                 <button type="submit" formaction="/talend" id="submit_tld" class="btn tal_btn btn-md btn-secondary" onClick="return saveTalend()">Save Talend</button>
                  <input type="hidden" id="talend_se" value="0" name="nest_talend" />

` 如果您对此有任何参考或任何意见欢迎。 TIA。

【问题讨论】:

  • 究竟是什么不工作?
  • 我无法获取存储在服务器上的数据。

标签: javascript node.js post


【解决方案1】:

您似乎在代码中使用了shell.mkdir,但在任何地方都没有定义shell - 至少在您提供的示例代码中是这样。你为什么不改用fs.mkdir? 另外,你需要一个\,介于:base_pathapp_name

base_path + '\' + app_name

base_path = 'C:\Projects'.

更好的是,使用path.join 确保加入的路径不是特定于平台的:

您也可以使用:JSON.stringify(fields.nest_talend) 而不是.toString(),因为如果fields.nest_talend 是一个对象,它将返回一个等于:'[object Object]' 的字符串。

最后,您对fs. writeFileSync 的调用是错误的。由于文件将被同步写入,因此不需要回调。此外,第一个参数需要是要写入的文件位置。试试这个:

fs.writeFileSync('location-path/file.json', tal_js, 'utf8');

我认为在这些更改之后,您的代码将可以工作。

【讨论】:

  • 我在这一行收到一个错误,说回调必须是一个函数。保存 = fs.writeFileSync('location-path/file.json', tal_js, 'utf8');
  • 在 node shell 中试试这个:a = {a:1, b:2} fs.writeFileSync('path/data.json', JSON.stringify(a), 'utf-8'); 是否有效?
  • 不,这是我得到的错误。 a = {a:1, b:2} fs.writeFileSync('path/data.json', JSON.stringify(a), 'utf-8');在 line:1 char:9 + a = {a:1, b:2} fs.writeFileSync('path/data.json', JSON.stringify(a), ... + ~ 参数列表中缺少参数。在line:1 char:50 + a = {a:1, b:2} fs.writeFileSync('path/data.json', JSON.stringify(a), ... + ~ ',' 后缺少表达式。在line:1 char:51 + ... {a:1, b:2} fs.writeFileSync('path/data.json', JSON.stringify(a), 'ut ...
  • 我得到的这个控制台的值为 0。console.log("hocon" + tal_js);
  • 你只是将所有内容复制粘贴为一行吗??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-30
  • 2019-10-26
  • 1970-01-01
相关资源
最近更新 更多