Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

to send a text file to server using javascript

Tags:

javascript

I need to send a text file to server and get it saved. how can i do it using javascript???

like image 824
sulakshana Avatar asked Jan 11 '11 11:01

sulakshana


People also ask

Can JavaScript send data to server?

The XMLHttpRequest is a built browser object that is used to communicate with the server in pure JavaScript. You can send data to the server or receive data from the server using the XMLHttpRequest object without reloading the entire web page.

Can JavaScript write to a text file?

There is a built-in Module or in-built library in NodeJs which handles all the writing operations called fs (File-System). It is basically a JavaScript program (fs. js) where function for writing operations is written. Import fs-module in the program and use functions to write text to files in the system.

Can JavaScript interact with server?

JavaScript on the server is not only possible but also recommended in most cases. It's fast, scalable, reduces development costs while increasing the speed and code quality. Every member of your team can work on all parts of the project and implement features from start to end.


2 Answers

There are all sorts of security issues surrounding this. Would you be happy to visit a website that would upload a file from your machine to the server?

For a generic website, where users are likely to have their permissions set to deny this sort of access it isn't possible.

If by chance, you are looking to do this for an application where you have control over the security settings for its users, and that you can guarantee its Windows and IE, then it is possible by reading the file and passing the details by posting to the server. See the following link : http://www.javascripter.net/faq/reading2.htm

However when you move away from IE or Windows, then you are going to struggle.

like image 124
James Wiseman Avatar answered Oct 12 '22 22:10

James Wiseman


using ajax of course.

have a file on the server, PHP or ASP - depending on what your internet server is. this file will accept the text file (data and name), and should also check for size and if this file already exists or not, and if all is ok- it will save it, and return a string "OK"

on the client, javascript side, just send the information to the server using ajax, or HTTPREQUST object - there's plentty of documentation for that around. and if you get back a response of "OK" then you know that it sent well. even better: don't use HTTPREQUEST, but do dynmaic script tag insertion - where the source attribute of the script you're appending is that file on the server like:

var a = document.createElement('script');
a.type = 'text/javascript';
a.src = "http://server/serverFile.PHP?filename=XXX&data=LONG STRING OF DATA REPRESTING THE DATA TO BE SAVED PROBABLY LESS THAN 2K IN SIZE AND ALSO YOU SHOULD ESCAPE OR ATLEAST URIENCODE IT";
document.body.appendChild(a);

and on the server file, serverFILE.PHP:

    <?php
    // some code to save the request variable [data].
    // if all is ok:
    alert("ok")
    // or:
    result = "ok"
?>

get it?

note: you'll probably have a limit of less than 2K on the file size.

like image 27
alfred Avatar answered Oct 13 '22 00:10

alfred