Create a text file on client-side programatically
We have a Ruby-on-rails server-side deployment that needs to allow users to download a pre-compiled self-extracting Windows EXE file on their computer.
We also need to generate a text file dynamically (based on the users id) and deploy it somewhere on the client machine where the EXE can find it.
We have 2 options:
- Generate this text file on the linux server and SOMEHOW? embed it in the Windows EXE. This seems far-fetched
- Get the user to download the static EXE and somehow get the dynamic text information stored on the clients machine (either through an invisible download, cookies, vbscript or javascripting on client side).
Note that we have, for now, ruled out having 2 separate download links (1 for the exe and 1 for the dynamic text info) because of problems associated with that user experience (i.e. ugly UX, downloads could be stored on different paths etc).
I am not much of a web developer but after spending a few minutes on Goog开发者_JAVA技巧le, I think both solutions are ridden with problems and ugly.
Anybody's got a smart idea?
It is possible to simply append your text file to the exe. These additional bytes will be ignored by the operating system loader, but your program will be able to get them. This is how self-extracting EXEs work.
The exe, when run, opens args[0]
(which is its own executable) and seeks the necessary number of bytes from the end and then reads the text.
It is straightforward for your web-script to serve the bytes of the exe in the response, and then send also the text file in the same response.
If the text is not a fixed length, you can also send the length of the text as an integer at the very end of the stream. The exe then reads first this fixed-size length field, before seeking the appropriate number of bytes from the end for reading the variable part.
The various examples [msdn] of appending data to the EXE show parsing the EXE structure from the beginning to find where the data starts. Simply appending the length of the data last to the EXE avoids needing to understand the EXE structure.
A couple of ideas:
1) Zip the exe and text file on the server file to be unzipped into the same location (creating a self extracting zip (exe) would be better.
2) Provide a KEY/PIN CODE after the user downloads the exe. When the application is first run, detecting no text file, it asks for the PIN, the exe then calls a webservice and downloads the information to save to the text file. This can be a Guid for instance.
It is possible to embed the text file into the executable as a resource.
精彩评论