简体   繁体   中英

How can i make a txt file automatically download to computer in javascript

I built an HTML page that uses the HTML5 function navigator.geolocation.getCurrentPosition() .

The JavaScript writes the longitude, latitude and accuracy to the screen,
but I'm looking for a way to to put that data into a text file and download it automatically to a specific directory in my computer (Windows7) for a Python program to read it later.

  <script type="text/javascript">
    function showLocation(position) {
       var latitude = position.coords.latitude;
       var longitude = position.coords.longitude;
       var accuracy = position.coords.accuracy;
       document.write("LAT:" + latitude + "_LON:" + longitude+ "_ACU:" + accuracy);

    }

    function errorHandler(err) 
    {
        var x
        switch(error.code) 
        {
            case error.PERMISSION_DENIED:
                x = "User denied the request for Geolocation."
                break;
            case error.POSITION_UNAVAILABLE:
                x = "Location information is unavailable."
                break;
            case error.TIMEOUT:
                x = "The request to get user location timed out."
                break;
            case error.UNKNOWN_ERROR:
                x = "An unknown error occurred."
                break;
        }
        document.write(x)
    }   
     function getLocation(){
        if(navigator.geolocation){
           // timeout at 60000 milliseconds (60 seconds)
           var options = {timeout:60000,enableHighAccuracy:true};
           navigator.geolocation.getCurrentPosition(showLocation, errorHandler, options);
        }

        else{
           alert("Sorry, browser does not support geolocation!");
        }
     }

  </script>

You need to make either the submit a form or perform an ajax call. This cannot be done in pure javascript on the client side to my knowledge. If you like python then you can use a python web framework like these

The main problem here is the difference between client and server. You don't really want people to be able to write files to your computer from the website you are hosting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM