/    Sign up×
Community /Pin to ProfileBookmark

writting a file with JavaScript ?

Hello,

is it possible to write a file on the client side with JavaScript ?

Jean-Marie

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterApr 18.2005 — JavaScript [b]is[/b] a client-side language.

I'm not sure what you're trying to do, but it is possible.

A Server-Side program can write to a JS file or even an HTML file.

And JavaScript can communicate with the Server via the submitting of a form.
Copy linkTweet thisAlerts:
@AdamGundryApr 18.2005 — You can store information in cookies, but otherwise not without the use of ActiveX or a similar technology, which is both platform-dependent and requires authorisation from the user.

Adam
Copy linkTweet thisAlerts:
@Warren86Apr 18.2005 — <HTML>

<Head>

<Script language=VBScript>

Function saveThis(fileSpec)
isData = xferContent.innerHTML
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(fileSpec) Then
MsgBox "Test.txt already exists...", vbInformation, "Information"
Exit Function
End If
Set contentFile = fso.CreateTextFile(fileSpec,True)
contentFile.WriteLine(isData)
contentFile.Close
MsgBox "Successfully saved to Test.txt", vbInformation, "Information"
set fso = Nothing
End Function

Function appendThis(fileSpec)
isData = appendContent.innerHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set contentFile = fso.GetFile(fileSpec)
Set fsoStream = contentFile.OpenAsTextStream(8)
fsoStream.WriteLine(isData)
fsoStream.Close
MsgBox "Successfully appended to Test.txt", vbInformation, "Information"
Set fsoStream = Nothing
Set fso = Nothing
End Function

Function readThis(fileSpec)
Set fso = CreateObject("Scripting.FileSystemObject")
Set contentFile = fso.GetFile(fileSpec)
Set fsoStream = contentFile.OpenAsTextStream(1)
isData = fsoStream.ReadAll
readContent.innerHTML = isData
fsoStream.Close
Set fsoStream = Nothing
Set fso = Nothing
End Function

Function showInfo(fileSpec)
Set fso = CreateObject("Scripting.FileSystemObject")
Set contentFile = fso.GetFile(fileSpec)
strInfo = contentFile.Name & vbCrLf
strInfo = strInfo & "Created: "
strInfo = strInfo & contentFile.DateCreated & vbCrLf
strInfo = strInfo & "Last Accessed: "
strInfo = strInfo & contentFile.DateLastAccessed & vbCrLf
strInfo = strInfo & "Last Modified: "
strInfo = strInfo & contentFile.DateLastModified
MsgBox strInfo, vbInformation, "File Information"
Set contentFile = Nothing
Set fso = Nothing
End Function


</Script>

</Head>

<Body>

<center>

<Div id=xferContent> This is the text that will be saved. </Div>

<Div id=appendContent> And this is the text that will be appended to the file. </Div>

<br>

<input type=button value='Save to text file' onclick="saveThis('C:WINDOWSDesktoptest.txt')">

<br><br>

<input type=button value='Append to the file' onclick="appendThis('C:WINDOWSDesktoptest.txt')">

<br><br>

<input type=button value='Read from the file' onclick="readThis('C:WINDOWSDesktoptest.txt')">

<br><br>

<input type=button value='Display File Info' onclick="showInfo('C:WINDOWSDesktoptest.txt')">

<br><br>

<br><br>

</center>

<Div id=readContent Style='Font Size:16pt;Color:Blue;'></Div>

</Body>

</HTML>
Copy linkTweet thisAlerts:
@schneiderjauthorApr 18.2005 — Thanks Warren,

that exactly what I want to do. I copy your script, but it did not work : the function are unkown. I will read your code more accuratly tomorrow evening.

Jean-Marie
Copy linkTweet thisAlerts:
@Khalid_AliApr 18.2005 — using ActiveXControl on a webpage is just like opening up your front door wide opned and leave it like that for over night, and when some one steals stuff from home then act surprised that how did some one got into the house.

I think a real developer must let people know of whats best for them, like if you want to suggest an ActiveXControl, you must put in red letters that its piece of crap solution and it actually has potential of harming their computer.
Copy linkTweet thisAlerts:
@phpnoviceApr 19.2005 — Thanks Warren,

that exactly what I want to do. I copy your script, but it did not work : the function are unkown.[/QUOTE]

If you're not using IE, then the code provided will not work at all. That code is written in VBScript -- not JavaScript. IE is the only browser that will execute VBScript. This VBScript statement:
<i>
</i>Set fso = CreateObject("Scripting.FileSystemObject")

becomes this JScript (similar to JavaScript but, again, IE-only) statement:
<i>
</i>fso = new ActiveXObject("Scripting.FileSystemObject");
Copy linkTweet thisAlerts:
@schneiderjauthorApr 19.2005 — thank you Khalid Ali...

that why this script did not work with my browser : I use Firefox. I did not want to use ActiveXControl, so what can you suggest me to solve my problem ?
Copy linkTweet thisAlerts:
@jbotApr 19.2005 — I use Firefox. I did not want to use ActiveXControl, so what can you suggest me to solve my problem ?[/QUOTE]

there is no equivalent for doing this in FF. again, why do you want to write to a file on the users' machines - are you coding malware, per chance?
Copy linkTweet thisAlerts:
@phpnoviceApr 19.2005 — ... are you coding malware, per chance?[/QUOTE]
There are valid and legitimate reasons for wanting to write to the visitors harddrive. However, the only secure and acceptable method of doing so is to use server-side code to send an HTTP attachment to the browser and let the browser raise the [b]Save As[/b] dialog box to ask the visitor where they would like to write the file. At that point, of course, the visitor has the right to refuse the file altogether.
Copy linkTweet thisAlerts:
@jbotApr 19.2005 — There are valid and legitimate reasons for wanting to write to the visitors harddrive[/QUOTE]

no, there aren't, save for cookies which should only be used to keep state. attempting to write to the users' HDDs suggests nefarious activity. sending attachments is not writing to the user's disk.
Copy linkTweet thisAlerts:
@phpnoviceApr 19.2005 — attempting to write to the users' HDDs suggests nefarious activity. sending attachments is not writing to the user's disk.[/QUOTE]
I disagree with both of your statements -- and, ya know, I am allowed to do that. :p Attempting to write directly to the visitor's harddrive suggests only a lack of knowledge about the restrictions concerning same. Also, since sending an attachment normally and usually results in a file getting written to the visitor's harddrive, any distinctions you attempt to bring to bear are merely splitting hairs.
Copy linkTweet thisAlerts:
@jbotApr 19.2005 — I disagree with both of your statements -- and, ya know, I am allowed to do that. [/QUOTE]

fancy that ... :rolleyes:


Attempting to write directly to the visitor's harddrive suggests only a lack of knowledge about the restrictions concerning same. [/QUOTE]

it doesn't suggest "only" that. it suggests [B]either[/B] the user is unaware of the restrictions (your argument which is valid), or that the developer is trying to evade security to deploy a nasty payload onto the end user's machine (my argument which is very definitely valid given the huge incidences of malware).


Also, since sending an attachment normally and usually results in a file getting written to the visitor's harddrive, any distinctions you attempt to bring to bear are merely splitting hairs.[/QUOTE]

it's not splitting hairs, however, since the distinction between directly writing to the HDD (ie via a script) and indirectly (the application does it response to a user request) is fundamental in terms of the developer's intentions. of course, sending an HTTP attachment without user interaction, ie as the result of a button being clicked, in the hope that they inadvertently click "yes" or "install" or whatever, is just as dodgy as the code-route since it purposefully attempts to bypass the end-users' defences.
Copy linkTweet thisAlerts:
@phpnoviceApr 19.2005 — First of all, I'll point out that I don't completly disagree with everything you're trying to say. The problem is that you're taking a hard (close minded) line and ignoring other perfectly valid aspects. For example:

<scenario>

If I were close to being a JavaScript neophyte and wanted to provide a relatively simple on-line WYSIWYG HTML editor, I could use JavaScript (and a ContentEditable DIV) to do so. Natuarlly, I would want the visitor to be able to save the HTML which they designed using my WYSIWYG HTML Edititor. Lacking knowledge of the restrictions on writing to the client harddrive via script, I would quite naturally, and of a necessity, start trying to find out how to allow the visitor to save the internally-generated HTML via script.

</scenario>

Obviously, I am not so naive to think that [b][u][i]nobody[/i][/u][/b] is ever going to want to write to the client harddrive for malicious purposes. By the same token, I believe it is irresponsible and immature to automatically jump to the obviously erroneous conclusion that just because a person is asking how to write to the client harddrive they must needs be up to no good.

Thus, when people ask, leave out the accusations and just answer the question. In this case, there are two perfectly legitimate answers.
[list]
  • [*]In an [u]intra[/u]net environment, browser security may be manually turned off, at (and from) the client end, in order to allow JavaScript (IE JScript, actually) to write to the client harddrive. Furthermore, this [u][i]does not[/i][/u] compromise that browser's security when it is on the Internet -- at least, not with IE. ?

  • [*]In an [u]Inter[/u]net environment, server-side code is required to send an HTTP attachment that will be written to the client harddrive.
  • [/list]
    Copy linkTweet thisAlerts:
    @schneiderjauthorApr 19.2005 — My objective was not to write some malware or virus or other think : I am only beginning to learn the use of an applet and JavaScript and so one...

    I have to send informations from the main page write in PHP (where are included the applet) to the Java server to performe calculation on these datas before saving them into the database (MySQL).

    So the way I think (or now I was tinking), was not elegant, but that the way I found (cookies are not relevant because the files could be to large).
    ×

    Success!

    Help @schneiderj spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.1,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...