How to uncompress php's gzcompress in javascript? - javascript

I am making an android app and a website that downloads info from a php script. The php script compresses the data using gzcompress($dat, 9);.
I know how to uncompressed it in android, but how do you do it in javascript?
In this post:
Compress data in php and uncompress in javascript
sstringer mentions you can put a special header when using php's gzencode so jquery can automatically uncompress it. Is there a way for the way I am doing it?
Thanks

I looked into this myself once. I decided to go to the ajax route and then redirect.
If you know where the file is being stored on the server you could post that to a php script which decompresses it and saves the file somewhere. Next, return this in the ajax response and then just redirect the user to the saved, decompressed file.
There is a project on GitHub: https://github.com/gcanu/gzip.js
That might be what you are looking for.

Related

How to use openstack on client side

I try to make a upload/download services of files for my website, and i'm trying to use the object storage from openstack. The thing is, i have no problem doing it via php and openstack php sdk, but when i'm trying to do it via some javascript, i can't find a good sdk or methods.
I'm not using node, I have a php server, and a javascript client. I would like to uploads or download files directly from the javascript client. I don't want the file to transfer through the php server. I managed to create openstack tokens with the php sdk, maybe i could send those to the javascript so then they can authenticate? It's been one week of searching with no solutions...
Openstack has a S3 plugin that can ease your search for a library/sdk.
Otherwise, you should forge a temporary URL server-side, I'm sure your PHP library has tooling for this. The URL can then be used client-side to PUT the file.
The temporary URL is forged in a way to open temporary write-only access for the upload. There is also the same kind of URL to open read-only access to some elements.
So, either the client requests for a place to upload to the PHP which sends back the URL, or simply have the client upload the file to your PHP that will forge the link and then redirect the request to the URL.

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?

AJAX Download large file and POST to other domain

I'm having issues with a small script I'm creating. It downloads large(ish) files from one domain (around 15Mb each) using AJAX, and I want to then upload / post them to another domain also using AJAX. Downloading isn't a problem. I've added a nice download status bar, and have checked that the responseText is stoed in a variable, which works fine.
The script then attempts to use AJAX POST to upload this data to another domain. I've set the 'Access-Control-Allow-Origin:' header on my domain, and the domains can both 'talk' to each other fine. Is it the AJAX that's not letting me upload such a large chunk of data? It actually crashes the browser.
Any ideas on how I should handle large files downloading then uploading straight away, would be greatly appreciated.
EDIT:
I'm trying to download a file from domain A to the users computer (but not save the file, just store it in a variable), then upload it straight to domain B.
Thanks! - Dan.
I would skip the downloading of the data through AJAX.
If i'm understanding what you're trying to do. You could download the data in your PHP, showing the progress using AJAX. Then within the same PHP code on the backend you could send the data to the next location. You could even send an signal through AJAX to tell your client side code that the download had finished and the sending had begun and have a progress bar for that.

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.

Open a text file from server on the Client-side using Javascript

No matter how much I look this up all I get is the w3C File API which focuses on local files for the client.
What I'm trying to do is I have a server. I'm trying to use client-side javascript to grab the server hosted text file, a.txt, and display it to the innerDOM of an html page. My server directory look like this:
index.html
read.js
text files
a.txt
All I want to have happen is for, on the client side, the javascript read.js running in the index.html on onload to display the contents of a.txt. I figure that since a.txt will never be large, leaving it to the client is fine.
But I can't figure out how to do this and the W3C File API isn't offering me answers.
If I had to guess, somehow making sure index.html loads a.txt and then grabbing that via the file API might be the way to go but I'm not sure how to do that.
And I'll admit it, I'm a bit of a noob. If I'm invalidating browser sandbox or doing something impossible, please tell me. I just thought this would be so simple.
Also, I'd appreciate that if you were going to suggest AJAX, either don't, or explain it like I'm a baby because I really don't know.
Thank you all so much for your help.
Why file API is irrelevant:
Web applications should have the ability to manipulate as wide as possible a range of user input, including files that a user may wish to upload to a remote server or manipulate inside a rich web application.
From W3C File API.
So, File API is intended to be used to allow users to upload files from their clients into the server. On the other hand, AJAX is used to allow users to download files and other data from the server into their clients. And this is exactly what you need.
Refer to jQuery's ajax documentation.
I believe this page should help you out with your problem.
http://www.htmlgoodies.com/beyond/javascript/read-text-files-using-the-javascript-filereader.html#fbid=YhNukIHynD3
I would suggest using an Ajax call to the file on the server, since the response of the call will typically be the contents of that file.
Using Jquery this can be done by a simple
$.ajax({ 'url':'a.txt',
'success': function(r){
//display to innerDOM here, using r as the file
});
});
You simply want to display a txt file on the web page?
Do you know about server side includes?
That would be one possibility if you control the server.
If you really want to do it in javascript, then AJAX would be the way to go.
If it were me at that point I would figure out how to include and use jQuery to help with the ajax bits.
You will simply request the text file via its URL (you can get it to load in the browser right?), and then use jQuery to put that text into some DOM element.

Categories