[英]Windows user name variable and javascript
几周前我问了这个问题 ,最近又回来解决了。 我有一个隐藏的输入,看起来像这样:
<input style="display:none;" class="fileDialog" type="file" nwworkingdir="C:\Users\" nwsaveas/>
这将在给nwworkingdir
的目录下打开一个保存对话框。 我希望默认位置为用户的Pictures
目录,但我无法使用javascript动态获取系统用户名。 我需要用户名,因为路径通常是C:\\Users\\USERNAME\\Pictures. I have tried constants such as
C:\\Users\\USERNAME\\Pictures. I have tried constants such as
ssfMYPICTURES and
CSIDL_PROFILE and
C:\\Users\\USERNAME\\Pictures. I have tried constants such as
但是javascript无法识别它们,或者我使用了错误的C:\\Users\\USERNAME\\Pictures. I have tried constants such as
。
该项目位于node-webkit中,因此使用javascript,node.js或node-webkit API的解决方案应该可行。
面向未来读者的解决方案:
“ screen_shot.js”中的Node.js函数:
exports.getUserHome = function() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}
index.html中的标头js:
$(document).ready(function() {
var sc= require('screen_shot');
$('.fileDialog').each(function(i) {
$(this).attr('nwworkingdir', sc.getUserHome() + '\\Pictures');
});
});
var sc= require('screen_shot');
function wait(e) {
var page = $(e).closest('[data-role="page"]');
$('#close-popup').click();
page.find('.fileDialog').click();
}
function screen_shot(fn) {
html2canvas($('body'), {
onrendered: function(canvas) {
var img = canvas.toDataURL("image/png").split(',')[1];
sc.buildFile(fn, img);
}
});
}
$('["..."]').live("pagecreate", function() {
$(this).find('.fileDialog').change(function() {screen_shot($('.fileDialog').val() + '.png');});
});
$('[id^="..."]').live("pagecreate", function() {
$(this).find('.fileDialog').change(function() {screen_shot($('.fileDialog').val() + '.png');});
});
$('[id^="..."]').live("pagecreate", function() {
$(this).find('.fileDialog').change(function() {screen_shot($('.fileDialog').val() + '.png');});
});
对话框和按钮:
<input style="display:none;" class="fileDialog" type="file" nwworkingdir="" nwsaveas/>
<a href="#" onclick="wait(this);" data-role="button" data-theme="j">Save As</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.