I have MVC web application where user draws a shape on a map and the coordinates of it should be saved in Json file in user's computer, particular location, so that it can be accessed and displayed for the user later. I get the coordinates. But how can I write the coordinates to Json file from a Javascript function?
Asked
Active
Viewed 55 times
0
-
https://stackoverflow.com/help/how-to-ask & https://stackoverflow.com/help/mcve – Ankit Kumar Dec 06 '17 at 06:59
-
You can can create it on server and make client to download that file. – SᴇM Dec 06 '17 at 07:04
-
@SeM if is downloaded, then it's in downloads folder and it will hard to access it. I want to write to a particular already created Json file which is already created. – user9035132 Dec 06 '17 at 07:09
-
@user9035132 it will hard to access for whom? Where is that particular file created (client/server)? – SᴇM Dec 06 '17 at 07:11
-
Javascript running in a page served from a web server can't write to your local filesystem sorry. – Dylan Nicholson Dec 06 '17 at 07:20
-
You have no access to your clients file system! (if you did the web would not even exist). You must create it on the server (and the user can choose to download to whatever location they want) – Dec 06 '17 at 07:21
-
@SeM Json file would be like my database. It is just a file in a folder in my visual studio project. I need to save data to json file so that I can later display information in a browser from it. So it would be getting information from browser, writing to a particular file and later again displaying in the browser. If file will be downloaded, it will be hard to find where it is downloaded in user's pc, so I won't be able to get data for displaying to browser. – user9035132 Dec 06 '17 at 07:21
-
@user9035132 it will not be harder to find it, you just can't access it. If you store your file on server and let client to change its content, it will work fine, can't see problem there. – SᴇM Dec 06 '17 at 07:24
-
You could use the browsers local storage (explained here: https://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage) – richej Dec 06 '17 at 07:33
1 Answers
0
Here's a way to write to a text file using FileSystemObject Reference (Windows Scripting) and JavaScript. See CreateTextFile Method for more info.
function WriteToTextFile(text, path, overwrite) {
var ov = false;
var fso = new ActiveXObject("Scripting.FileSystemObject");
if(overwrite === true)
ov = true;
var a = fso.CreateTextFile(path, overwrite);
a.WriteLine(text);
a.Close();
}
Usage:
var obj =
{
propA: "A",
propB: "B",
propC: 123
};
var text = JSON.stringify(obj);
var path = "c:\\testfile.txt";
WriteToTextFile(text, path, false);
Note that you need to install the ActiveX plugin and this only works on IE. What you want to achieve is not considered a good practice and poses security risks which is why the ideal way is to generate the file on the server and prompt the user to save it. I also don't see why you are using web with a local database. Basing on your requirement, I'd suggest using localStorage.

jegtugado
- 5,081
- 1
- 12
- 35