Is it possible to download file to the server using JavaScript - javascript

Every time user click specific button, my javascript code needs to send request to the different server, and download a file to my server, than process it.
Is it possible to do?

JavaScript by itself would not be able to do this, as it is a Client-side scripting language, meaning it runs entirely on the user's browser and has little to do with the server itself.
However, it would be entirely possible to do this with PHP (or any other server-side language), and then have JavaScript tell the server to run the PHP script to download and process the file.

Related

Start python script from client side

I'm trying to start a python script that will parse a csv file uploaded from the UI by the user. On the client side, how do I make a call to start the python script (I've read AJAX http requests work)? And then secondly, how do I take the user input (just a simple user upload with the HTML tag) which will be read by the python script?
The back end python script works perfectly through the command line, I just need to create a front end for easier use.
Many (if not all) server side technology can solve your problem: CGI, Java Servlet, NodeJS, Python, PHP etc.
The steps are:
In browser, upload file via AJAX request.
In server, receive file sent from browser, save it somewhere in server disk.
After file is saved, invoke your python script to handle the file.
As your current script is written by Python, I guess Python is the best choice for server side technology.

Modifying Text file in javascript

I need to have data written to a text file in javascript. I want it to write a username and password to the text file and create a new line every time. Here is my code http://pastebin.com/24Tvdemu.
Can anyone help this has had me stumped for ages.
As Javascript in html is a client side language, you will need to send the files to the server, and save there the file. Anyway, you can prompt the user to save the file in their local machine, but it´s not usefull at least you really need that for any reason.
Check this answer Javascript: Create and save file
Some suggestions for this -
If you are trying to write a file on client machine, You can't do this in any cross-browser way. IE does have methods to enable "trusted" applications to use ActiveX objects to read/write file.
If you are trying to save it on your server then simply pass on the text data to your server and execute the file writing code using some server side language.
To store some information on the client side that is considerably small, you can go for cookies.
Using the HTML5 API for Local Storage.
More details : Is it possible to write data to file using only JavaScript?

If HTML, CSS, and Javascript are client-side, why are they components of a PHP file?

I often hear of the term server-side and client-side programming in regards to web development. They say that server-side and client-side are in a way decoupled from each other. From my understanding, server-side programming makes use of PHP, Rails, Node, ASP.NET, etc., as the technologies and client-side programming makes use of HTML, CSS, Javascript, etc.
Here is where I am fundamentally confused.. From what I know, a PHP file can include HTML, CSS, and Javascript... My question is:
If server-side and client-side programming are indeed separate, why does PHP include HTML, CSS and Javascript? If all of these are done in PHP, the server, where does the client come in? In a typical website run on a PHP server, will there be standalone HTML, CSS, and Javascript files that are not PHP files? Will the client-side developer have have edit the HTML, CSS, and Javascript parts of the PHP file, while the server-side developer works on the PHP part of the file?
Your question sure is a good one many people asked sometime in their lives(web developers).
PHP indeed is a server side script, but the .php extension acts like a normal .html file most of the time.
PHP needs to be a partner with JS and HTML to work.
E.g. A login form. First, the client has completed the form and submitted it. JS then comes in power, using ajax to send your login information to the server(It could be the same document xxx.php, but the server only cares about the php script part).
Then, it sends back a result from the server and may insert a snippet of JS into your login form, where JS empowers and redirect the user from their HTML interface to a new website.
As you can see from the above example, clients and server handles a webpage differently disregarding their file extension. Clients cannot download the PHP source code and the PHP server doesn't care about other than php code themselves.
A single web file is like a port, where clients send information to a php page and the server returns a snippet.
Clients and servers may use one single .php page or can refer to different pages, but the server side webpage is always unaltered
If server-side and client-side programming are indeed separate, why does PHP include HTML, CSS and Javascript?
So it can compactly pack small things inside one web page. Clients view the interface, server executes the PHP code. However, it is not necessary to pack everything into one webpage.
Also, the .php extension can be viewed by clients, so that they know they will interact with the server sometime on that page. Also, .php does not necessary need to include PHP code.
If all of these are done in PHP, the server, where does the client come in?
Clients need to use JS to send information to the server for its response.
On a typical webpage running on a PHP, will there be standalone HTML, CSS, and JavaScript files that are not PHP files?
Yes, files that need not to be parsed by the PHP engine can be named and stored as standalone HTML, CSS and JavaScript file.
Will the client-side developer have have edit the HTML, CSS, and Javascript parts of the PHP file, while the server-side developer works on the PHP part of the file?
I will rephrase your question to be "So the client-side browser can change the DOM, while the server works on the PHP part?". There is no "client sided developer. There is just client sided visitors"
Partially right. Clients download a webpage, not using the same file on the server, the webpage can be altered before sending to the clients. Clients don't get to read the PHP source code, the server is done running the PHP code before sending a webpage to the clients, so the two do not run together. When clients send queries to the server, the server executes nothing but PHP. The .php document is unaltered on the server. After the PHP server has responded, usually, they will send back information to the browser viewing that particular webpage and trigger JS code and change the webpage's DOM, which means the look of the webpage is modified. You can interpret it as "HTML, CSS and JS" being altered.
If all of these are done in PHP, the server, where does the client come in?
The WWW works on a server-client basis.
Web browsers ask web servers for resources. Web servers send those resources to the browser. The browser then interprets them.
When you use server side programming, you just generate those resources programatically instead of reading them from files.
So:
the PHP will run on the server and generate some output
the output it sent to the browser
the browser interprets the output
So the output has to be in a form that the browser understands.
In a typical website run on a PHP server, will there be standalone HTML, CSS, and Javascript files that are not PHP files?
Generally speaking, the CSS and JS will be in static files. The HTML contains the data which is likely to be dynamic (and then generated from PHP) so will probably come from PHP files (although they might use separate template files to get the HTML structure).
Will the client-side developer have have edit the HTML, CSS, and Javascript parts of the PHP file, while the server-side developer works on the PHP part of the file?
There are lots of different ways of working. If you get your server side logic sufficiently separated from from your client side code, then that is a viable way of working.
PHP needs to be executed on the server. The PHP usually determines which CSS/JS/HTML to export to the client. Therefore it is in the PHP file.
HTML, CSS and JS actually is executed on client side. The reason it is in your PHP file, is we need some way of delivering the code to the client. It doesn't just magically appear.
CSS and JS doesn't even have to be in the PHP file. You can use HTML inclusions to let the browser fetch it
eg.
<script src="/loc/of/js/file.js"></script> (JS)
<link href="/loc/of/css/file.css" rel="stylesheet"> (CSS)
Whene you use PHP page, means you want to execute some code in your server at the same time you can include some JS code , or CSS styles, but the server will not execute it.
All the PHP code will be exucuted by the Server.
CSS and JS code will be interprets by your browser because they're front-end code.
All PHP does is that it creates html pages which contain css and javascript code as well and sends it to client. Now how php creates a page differs in application. I hope it helps.

How to run javascript file on the server side?

Suppose I have server. A client loading an HTML file containing a javascript library will have the script executed by the browser. The problem here is that if the client's computer is slow, the processing will take a long time.
So I want to move the processing to the server side. But, instead of having to rewrite the entire javascript library into another language, I simply want to run the javascript on the server.
Googling "server side javascript" directs me to Node.JS, which in my imagination have the capability to do so. But, I cannot find a tutorial which does just that. Does this mean that there really is no easy way to do so? For example, because the javascript script may contain DOM specific things such as document.getElementById(), which does not make much sense on the server side.
There is no trivial way to simply shift processing of JS from the client to the server.
You need to break the code down into code that must run on the browser (such as, assuming you don't want the browser to load an entirely new page, DOM manipulation) and code that can run on the server.
Then you need to provide a way to pass data between the server and the browser, this is normally done via HTTP (using Ajax).
When you take input from the client you need to send it to the server in an HTTP request (instead of just passing it as an argument to a function). The server needs to read the HTTP request, process it, and make an HTTP response.
The Ajax callback then needs to parse the response and run any client side logic (such as DOM updates) in response.
Note that network communication times will impact performance.
You can't "merge" the client and server in this way. All you could do is process the data on the server and just display it in the client without any further processing. Maybe you should refresh you knowledge about HTTP and how websites are send to the clients. Without any additional tricks, like websockets, comet or ajax polling, you can't access the client after you send the initial website to it. Even than you can just send data to the client.
When you want to stick to Javascript, Node.js is a good option. But even than you would need to send the data you want processed to the server, process it there and send back the processed data in JSON or "display ready" HTML.

Cross Domain requests using JQuery

This is a followup question to the one here
Here's briefly what I am trying to do. The File server creates a text file to indicate an end of the process. On a webpage on the Web Server, I loop every x seconds and make an ajax request to find out if the test file exists (ajax request to http://fileserver/files/UserFile.txt)
I've tried the following approaches so far:
Trigger a web method from the client side that creates a HttpContext object to verify if the text file exists. But this is too strenous on the server and I started getting all kinds of exceptions in the Event Viewer.
YQL works great but unfortunately it's too slow. The user waits atleast twice the amount of time.
I am looking for a solution that doesn't involve the server side. Somehow, I'd like to use JQuery to verify the existence of a text file on the fileserver.
Any thoughts?
You should be able to use JSONP and JQuery.ajax() to do cross-domain request work. Play with the jsonp and jsonpCallback attributes. Alternatively, you can use JQuery.getJSON().
Serving a single file from the filesystem is the most simple operation a web server can do. If that is already too much, then all other solutions will be worse. Find out why the server takes so long to serve a simple file and fix that.
Note: I'm assuming that the file is small since you say "test file". If it's a big file, the server will actually send it to the client which will need a lot of resources.
What you can try is to add an ASP page to the web site which runs code on the server that checks whether the file is there and just returns a tiny piece of HTML which you can add to the page with jQuery.load().
I may be miles off base here but... could you not create ONE asynchronous (!) Ajax client request with a HUMONGOUS timeout. Fire it, and wait. You would be invoking some server script that checks every so often, in a loop on the server (using sleep in between), whether the file exists. And not replying to the Ajax request until the file finally shows. The server script then replies and exits.
EDIT: Depending on the server-side scripting framework used, you may even get some OS support. You may be able to sleep on a status change in the directory...

Categories