admin管理员组文章数量:1292244
I would like to give the users in my website the ability to download a "lnk" file. My idea is to generate this file with to contain an address that can be used only once. Is there a way to generate this file in javascript? The flow is something like -
- the user presses a button
- the javascript generates this file and downloads it to the user's machine
- the user sends this file to another user to use this one-time-address from his machine
Is something like this is doable in javascript from the client side? or would i need to generate this file using java server side?
I would like to give the users in my website the ability to download a "lnk" file. My idea is to generate this file with to contain an address that can be used only once. Is there a way to generate this file in javascript? The flow is something like -
- the user presses a button
- the javascript generates this file and downloads it to the user's machine
- the user sends this file to another user to use this one-time-address from his machine
Is something like this is doable in javascript from the client side? or would i need to generate this file using java server side?
Share edited Sep 2, 2014 at 11:15 Vishwanath 6,0046 gold badges40 silver badges57 bronze badges asked Sep 2, 2014 at 11:11 user1322801user1322801 8591 gold badge13 silver badges28 bronze badges 10- check this codeproject./Articles/55488/File-Download-Using-JavaScript – mck Commented Sep 2, 2014 at 11:21
-
1
@Cory sure it does - check out
Blob
, you can create arbitrary files in JS and download them to the puter. – Benjamin Gruenbaum Commented Sep 2, 2014 at 11:22 -
5
@user1322801 here is how you'd do it - not gonna be a lot of fun. First - read how the
.lnk
file format works Microsoft released the specification. Second use aBlob
to write that binary data in JavaScript. Finally convert that Blob into a download for all browsers for example see this page or stackoverflow./questions/3665115/… – Benjamin Gruenbaum Commented Sep 2, 2014 at 11:25 - 1 @TomSarduy I don't believe so since it does not specify how to exactly acplish any of those things. All it does is give OP (and possible future answers) directions on how to attempt to solve the problem - an answer should be an actual solution to the problem (not production ready, but at least at a proof of concept level). I think a proof of concept is about 80 lines of code (maybe a bit less) and would round a nice amount of reputation for whoever writes it if you feel like picking up the glove and implementing it. – Benjamin Gruenbaum Commented Sep 2, 2014 at 11:33
- 1 @BenjaminGruenbaum: The OP did ask "Is it doable? Is there a way to generate files in JS?" not "How to create lnk files?". Your ment does indeed answer the question - I think asking for an actual solution would be off-topic or too broad. – Bergi Commented Sep 2, 2014 at 11:38
4 Answers
Reset to default 8This is a faithful translation of mslink.sh.
I only tested my answer in Windows 8.1, but I would think that it works in older versions of Windows, too.
function create_lnk_blob(lnk_target) {
function hex_to_arr(s) {
var result = Array(s.length / 2);
for (var i = 0; i < result.length; ++i) {
result[i] = +('0x' + s.substr(2*i, 2));
}
return result;
}
function str_to_arr(s) {
var result = Array(s.length);
for (var i = 0; i < s.length; ++i) {
var c = s.charCodeAt(i);
if (c >= 128) {
throw Error("Only ASCII paths are suppored :-(");
}
result[i] = c;
}
return result;
}
function convert_CLSID_to_DATA(s) {
var idx = [[6,2], [4,2], [2,2], [0,2],
[11,2], [9,2], [16,2], [14,2],
[19,4], [24,12]];
var s = idx.map(function (ii) {
return s.substr(ii[0], ii[1]);
});
return hex_to_arr(s.join(''));
}
function gen_IDLIST(s) {
var item_size = (0x10000 + s.length + 2).toString(16).substr(1);
return hex_to_arr(item_size.replace(/(..)(..)/, '$2$1')).concat(s);
}
var HeaderSize = [0x4c, 0x00,0x00,0x00],
LinkCLSID = convert_CLSID_to_DATA("00021401-0000-0000-c000-000000000046"),
LinkFlags = [0x01,0x01,0x00,0x00], // HasLinkTargetIDList ForceNoLinkInfo
FileAttributes_Directory = [0x10,0x00,0x00,0x00],
FileAttributes_File = [0x20,0x00,0x00,0x00],
CreationTime = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
AccessTime = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
WriteTime = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
FileSize = [0x00,0x00,0x00,0x00],
IconIndex = [0x00,0x00,0x00,0x00],
ShowCommand = [0x01,0x00,0x00,0x00], //SW_SHOWNORMAL
Hotkey = [0x00,0x00], // No Hotkey
Reserved = [0x00,0x00],
Reserved2 = [0x00,0x00,0x00,0x00],
Reserved3 = [0x00,0x00,0x00,0x00],
TerminalID = [0x00,0x00],
CLSID_Computer = convert_CLSID_to_DATA("20d04fe0-3aea-1069-a2d8-08002b30309d"),
CLSID_Network = convert_CLSID_to_DATA("208d2c60-3aea-1069-a2d7-08002b30309d"),
PREFIX_LOCAL_ROOT = [0x2f],
PREFIX_FOLDER = [0x31,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
PREFIX_FILE = [0x32,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
PREFIX_NETWORK_ROOT = [0xc3,0x01,0x81],
PREFIX_NETWORK_PRINTER = [0xc3,0x02,0xc1],
END_OF_STRING = [0x00];
if (/.*\\+$/.test(lnk_target)) {
lnk_target = lnk_target.replace(/\\+$/g, '');
var target_is_folder = true;
}
var prefix_root, item_data, target_root, target_leaf;
if (lnk_target.substr(0, 2) === '\\\\') {
prefix_root = PREFIX_NETWORK_ROOT;
item_data = [0x1f, 0x58].concat(CLSID_Network);
target_root = lnk_target.subtr(lnk_target.lastIndexOf('\\'));
if (/\\\\.*\\.*/.test(lnk_target)) {
target_leaf = lnk_target.substr(lnk_target.lastIndexOf('\\') + 1);
}
if (target_root === '\\') {
target_root = lnk_target;
}
} else {
prefix_root = PREFIX_LOCAL_ROOT;
item_data = [0x1f, 0x50].concat(CLSID_Computer);
target_root = lnk_target.replace(/\\.*$/, '\\');
if (/.*\\.*/.test(lnk_target)) {
target_leaf = lnk_target.replace(/^.*?\\/, '');
}
}
var prefix_of_target, file_attributes;
if (!target_is_folder) {
prefix_of_target = PREFIX_FILE;
file_attributes = FileAttributes_File;
} else {
prefix_of_target = PREFIX_FOLDER;
file_attributes = FileAttributes_Directory;
}
target_root = str_to_arr(target_root);
for (var i = 1; i <= 21; ++i) {
target_root.push(0);
}
var id_list_items = gen_IDLIST(item_data);
id_list_items = id_list_items.concat(
gen_IDLIST(prefix_root.concat(target_root, END_OF_STRING)));
if (target_leaf) {
target_leaf = str_to_arr(target_leaf);
id_list_items = id_list_items.concat(
gen_IDLIST(prefix_of_target.concat(target_leaf, END_OF_STRING)));
}
var id_list = gen_IDLIST(id_list_items);
var data = [].concat(HeaderSize,
LinkCLSID,
LinkFlags,
file_attributes,
CreationTime,
AccessTime,
WriteTime,
FileSize,
IconIndex,
ShowCommand,
Hotkey,
Reserved,
Reserved2,
Reserved3,
id_list,
TerminalID);
return new Blob([new Uint8Array(data)], { type: 'application/x-ms-shortcut' });
}
var blob = create_lnk_blob('C:\\Windows\\System32\\Calc.exe');
Use it like:
var blob_to_file = create_lnk_blob('C:\\Windows\\System32\\Calc.exe');
var blob_to_folder = create_lnk_blob('C:\\Users\\Myself\\Desktop\\'); // with a trailing slash
Demo: http://jsfiddle/5cjgLyan/2/
I've written a script based off of Gary's ment. Github link: https://github./HCR-Law/Lnk-File-Generator
It is written in TypeScript and is built to run in Bun, but could easily be rewritten for Node or for the browser.
To run it just import the exported default function and pass in the typed parameters. Example:
const file = createLinkFile({
linkTarget: "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
});
await Bun.write(`${outdir}/test.lnk`, file);
This would be simple if your website allows php.
If your script is part of an html file, just write the the javascript as if you were writing it to send a static lnk file. Then, at the lnk address part, break apart the javascript into two parts, breaking into html. Then at that point, put in
<?php /*PHP code set a variable *? /* PHP code to generate proper string*/ PRINT /*PHP variable*/
?>
I think make it pure client is impossible. Even the web rtc protocol need at least one iceServer to signal other client.
And I think the easiest way to do that is use http://peerjs./
you could first create a clinet token of the room owner
//room owner side
peer.on('open', function(my_peer_id) {
console.log('My peer ID is: ' + my_peer_id);
});
And send the token to any other you want (by text file, web chat ...etc)
Then other connect it use the token above
//the other one
var conn = peer.connect(other_peer_id);
After the room owner detected someone entered the room.
Disconnect from signal server, so the token will bee unusable
//room owner side
peer.disconnect()
About generate and read file by client side, I remend you read article below.
- http://www.html5rocks./en/tutorials/file/dndfiles/ read from file
- How to use filesaver.js save as file
I believe the patibility of fileReader api and blob doesn't matter. Since there will never be a browser which support webrtc but not support fileReader api
本文标签: htmlIs there a way of Creating lnk file using javascriptStack Overflow
版权声明:本文标题:html - Is there a way of Creating lnk file using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741551876a2384927.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论