【发布时间】: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