AS3 compress a string to send it in POST protocol - actionscript-3

I'm developing a flash binary that execute an animation in some web pages, and during this time I want to get the current source code of the page and send it via a POST call.
To get current source code I execute some javascript like this :
src = ExternalInterface.call("function(){return document.documentElement.outerHTML;}");
At this point, no problem.
After that, as the source code could be very huge, I wanted to compress it, so I tried to work with BiteArray like this :
var dataSrc:ByteArray = new ByteArray();
dataSrc.writeUTFBytes(src); // xmlData is original XML string
dataSrc.compress();
At this point, how could I send this to some server script (here I use PHP) ?
I mean : I don't want to know how to send data to an URL (I will use URL Loader), but how should I set my variable ?
Directly dataSrc or data.toString() ?

After compressing with ByteArray, I just Base64Encode my result, transmit it to my server script, and in this script I Base64Decode and gzuncompress.
Why should I Base64Encode ? Because to transmit the data to the server, my data is converted in a string and doing this cause bit lose, and padding errors ... Base64Encode permits to convert each bit of my data in a valid string to communicate.

Related

Read and write to JSON on x10Hosting

I have an angular 2 project, how can I read and write to a JSON file on my server?
I can do what I want within my code itself bit I don't want to have to change my code, recompile and upload my website every time.
Any help? Examples are greatly appreciated
Angular can read the remote JSON file using the HTTP Client but it can't directly write to the remote file.
For writing, you can use a server side script such as PHP (supported by x10Hosting) to provide a url that allows Angular to post to (also using the HTTP Client), to update the JSON.
For example something like this PHP:
$data = json_decode('./data.json'); // decode the json
$data->something = $_POST['something']; // update the something property
file_put_contents('./data.json', json_encode($data)); // write back to data.json

Get last segment of URL path with MVC Razor

I have this URL: http://localhost:.../home/blogpost/#'I want to get this string'
I get it when pressing:
#item.Title
#item.Title is from my database and that string will change. Now I need to get the 'I want to get this string' string on that page so that I can do a if statement with it, like this:
#if(#item.Title == 'I want to get this string')
Any suggestions?
The target attribute (the part of an URI after the #) is not sent to the server when the browser retrieves the page. So, you cannot get it out of a normal GET or POST request on the server at all.
That data is available in the browser, so you could access it using javascript of even CSS.
Using ajax techniques, you could load a skeleton page and send the target string yourself to the server and have it react to it, and only then load the rest of the page. A bit overkill for most uses I'm afraid and there are drawbacks as well (e.g. search engine might have more trouble to see your content).
It's easier to send the string to the server as a GET parameter if you have the level of control you seem to have. [simply replace the # with a ?], that will be sent to the server by the browser.
Do note that you should URLencode any data you add on to a URL ...

Swift - Store JSON globally

i have this JSON array stored in my local variable:
let bigJsonArray = JSON(response)
my question is if there is any possibility to store this "bigJsonArray" in a global variable/session/cookie/config so i can access it in every view of my app ?
Anybody knows how to process this and could help me?
Greetings and thanks!
What you can do is to define bigJsonArray as a global variable just by defining it outside of any class and the Swift compiler will understand it as a global variable and you can access it from anywhere in your code.
for example:
import UIKit
var bigJsonArray = JSON(response)
class a {
var x = 0
}
that's of curse will not save the data if you killed the app, but from what I understand from your question you just need to be able to access it from all the app without resending a request to the server.
If you want to save the JSON data permanently, you just store the data that you received as a file, and the next time you need it, you read it from the file and parse it (there's actually a method for that) instead of downloading and parsing the data. Much easier than trying to store the parsed data.
If this is data that can be downloaded again, read the appropriate documentation to make sure the file isn't backed up, and is stored in a cache directory where the OS can remove it if space is tight.

Accessing the DB in Cloudant with Angular

I am attempting to connect to a DB on Cloudant. I created the DB from a JSON and uploaded it to Cloudant here: DeanRadar
I copied the info byte for byte into another JSON on my server including the "_id" and "_rev". If I call the JSON directly it works fine, but I can not get the code to work when I enter the URL given above.
I am guessing the URL is just a place holder and it gets resolved before returning it to my browser, like it is supposed to have a ".json" on the end of it or something that the browser can read but not the code. I do not know enough about Cloudant, Couch, angular, etc to know what I am looking for. I have done plenty of PHP/SQL databases but this is my first attempt with cloudant/jason/angular.
Here is the code in question (ignore the fact that I declare dbURL twice, it is there to show what I have used)
var dbURL = 'https://deanradar.cloudant.com/tempdata/80d7d28d9e96778ad3bf4531817ab190';
var dbURL = 'data/tempData.json';
$http.get(dbURL).success(function(data) {
tempDataDeferred.resolve(data);
});
This works, when I access the JSON on my server copied from the cloudant DB, but swapping the dbURL lines (accessing the direct URL for the cloudant DB) returns nothing. You can take that same URL and slap it into your browser address bar and see the info exactly as it sits in the JSON.
Grabbing the "[API URL+]" address doesn't help, it won't even resolve in the browser. Any idea what I need to do to access the DB?
Check this document
https://cloudant.com/for-developers/crud/#browser-update
although in examples they are using jquery.couch you can check the original code and apply it to your angularjs app

pass a dataURL through AJAX

I'm attempting to save a dataURL made from an HTML5 canvas to my mysql db.
I have ajax setup to pull the var I've made called "dataURL" and I'm setting dataURL to the canvas's img using this code:
dataURL = oCanvas.toDataURL();
my issue is the canvas's dataURL has alot of characters that don't work well in pulling for ajax so I need a way to encode it or manipulate it differently so my end result can be saved to a mysql db and then later "decoded" to display once again.
I know my AJAX works because if I set the var dataURL to something like "cheese" it saves in the database as cheese.
Any help would be much appreciated!
The data you receive from toDataUrl will be in a format like this:
data:image/png;base64,/9j/4AAQSkZJRgABAQAAAQABAAD.... (very long string)
Firstly filter the data received to only the part after data:image/png;base64,
Then use whatever Base64 library your language provides to decode it to an array of bytes (or blob). If you are using Java: Apache Commons Codec
Here is an example in groovy:
def bytes = new Base64().decode(filteredData) as byte[]
You can save the decoded result to your database (to be retrieved later)
I actually ended up setting an event so when the image was done drawing it would set the dataURL to a hidden form element. No longer needed to pass it through ajax :). Thanks for the suggestion though.
Base64 encoding takes almost 4x as many bytes to encode an image. It is suggested to use window.atob to decode the base64 before transmission whether you are using ajax or regular forms. Of course IE doesn't support it but this looks like a shim.