Generate some xml in javascript, prompt user to sa

2019-02-02 01:02发布

I'd like to make an XML document in JavaScript then have a save dialog appear.

  1. It's OK if they have to click before the save can occur.
  2. It's *not* OK if I *have* to use IE to achieve this (I don't even need to support it at all). However, Windows is a required platform (so Firefox or Chrome are the preferred browsers if I can only do this in one browser).
  3. It's *not* OK if I need a web server. But conversely, I don't want to require the JavaScript to be run on a local file only, i.e. elevated privileges -- if possible. That is, I'd like to to run locally or on a *static* host. But just locally is OK.
  4. It's OK to have to bend over backwards to do this. The file won't be very big, but internet access might either be there, be spotty or just not be a possibility at all -- see (3).

So far the only ideas I have seen are to save the XML to an iframe and save that document -- but it seems that you can only do this in IE? Also, that I could construct a data URI and place that in a link. My fear here is that it will just open the XML file in the window, rather than prompt the user to save it.

I know that if I require the JavaScript to be local, I can raise privileges and just directly save the file (or hopefully cause a save dialog box to appear). However, I'd much prefer a solution where I do not require raised privileges (even a Firefox 3.6 only solution).

I apologize if this offends anyone's sensibilities (for example, not supporting every browser). I basically want to write an offline application and Javascript/HTML/CSS seem to be the best candidate considering the complexity of the requirements and the time available. However, I have this single requirement of being able to save data that must be overcome before I can choose this line of development.

9条回答
对你真心纯属浪费
2楼-- · 2019-02-02 01:46

Using a base64 encoded data URI, this is possible with only html & js. What you can do is encode the data that you want to save (in your case, a string of XML data) into base64, using a js library like jquery-base64 by carlo. Then put the encoded string into a link, and add your link to the DOM.

Example using the library I mentioned (as well as jquery):

<html>
<head>
    <title>Example</title>
</head>
<body>
    <script>
        //include jquery and jquery-base64 here (or whatever library you want to use)
        document.write('<a href="data:application/octet-stream;base64;charset=utf-8,' + $.base64.encode( "this is a example, which requires the jquery-base64 library to work... replace this text with your xml" ) + '">click to make save dialog</a>');
    </script>
</body>
</html>

...and remember to make the content-type something like application/octet-stream so the browser doesn't try to open it.

Warning: some older IE versions don't support base64, but you said that didn't matter, so this should work fine for you.

查看更多
Summer. ? 凉城
3楼-- · 2019-02-02 01:46

Are you looking for something like this?

If PHP is ok, if would be much easier.

查看更多
Anthone
4楼-- · 2019-02-02 01:54

Javascript is not allowed to write to a local machine. Your question is similar to this one.

I suggest creating a simple desktop app.

查看更多
登录 后发表回答