AS3 POST png to PHP - actionscript-3

Have read many similar but no solutions: I'm trying to do something (that i think is) really simple,
Take a photo from gallery or camera - and POST this to a URL. No saving / returning to application. The PNG is just used by the webpage in display and user moves onward.
public function postPicture():void {
PNGEncoder2.level = CompressionLevel.FAST;
var encoder : PNGEncoder2 = PNGEncoder2.encodeAsync(_bitmapData);
encoder.targetFPS = 12;
encoder.addEventListener(Event.COMPLETE, function (e):void {
var png : ByteArray = encoder.png;
var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var jpgURLRequest:URLRequest = new URLRequest("https://myurl.com/mypage");
jpgURLRequest.requestHeaders.push(header);
jpgURLRequest.method = URLRequestMethod.POST;
jpgURLRequest.data = png;
navigateToURL(jpgURLRequest, "_blank");
});
}
Also to note, we're getting some odd characters on the end of the URL: C9%90NG
Hunch is that I'm decoding and then not encoding properly before sending i.e. these characters some part of raw image info.
When I test from chrome POST extension with another image, server side stuff works fine so guessing a problem with AS3.
basically its working when I use chrome to browse > my.png and POST it at the page. Am I missing something with the AS3 png encoder? i.e. how to turn that bmpdata into a 'file' rather than an array of bytes.
thankyou

You need to use multipart upload. It's a common situation with a well defined standards for that. You can implement it by yourself but it will take some time and efforts.
What I've used before is the Multipart URLLoader Class provided by Eugene Zatepyakin, one the best Flash guru's out there :)
It's some kind of old but does the job extremely well. You can add variables, files, whatever content you want to your loader and simply submit it. There are examples with the php side of the thing so you can understand what's going on.

Related

display only a specific content in AS3 code using webStage View

I'd like to display, in my app, only a part of a web page.
On this website, I'd like to display, in my app, only the div id "MovieCart".
What should I write in my as3 code in order to do so ?
For now, I have this line :
webView.loadURL("http://www.cinecity.nc/Cinecity/Film/40565");
But, of course, it's displaying the fullwebpage.
EDIT
So, I've tried this :
webView.addEventListener(Event.COMPLETE,onComplete);
var res : String = ExternalInterface.call("function(){return document.getElementById('movieCart').outerHTML}");
var urlOfMovie: URLRequest = new URLRequest("http://www.cinecity.nc/Cinecity/Film/40567");
var loaderMovie:URLLoader = new URLLoader();
loaderMovie.load(urlOfMovie);
webView.loadString(res);
But, as it's an AIR app, ExternalInterface.call can't be call. Any idea ?
Here is one easy way you can accomplish this:
//First, load the full page as you're currently doing:
webView.addEventListener(Event.COMPLETE, webLoadComplete); //listen for when the load is finished
webView.loadURL("http://www.cinecity.nc/Cinecity/Film/40565");
//runs when the load finishes
function webLoadComplete(e:Event):void {
webView.removeEventListener(Event.COMPLETE, webLoadComplete); //stop listening
//second, invoke the following Javascript on the page which assigns the `MovieCart` element as the html for the whole document body
webView.loadURL("javascript:document.body.innerHTML = document.getElementById("MovieCart").outerHTML");
}
Disclaimer:
Keep in mind that scrapping content from websites is generally frowned upon and you may be infringing on peoples work/copyrights by doing so.

CefSharp and frames, only retrieving HTML from first frame

pcpao.org/general.php?strap=152814186280001650
In trying to get the full HTML from that site, .GetSourceAsync and .ViewSource, both only show the 'frameset' HTML. Using the ShowDevTools option, the full HTML data is in both the elements collection and the Sources of the Chrome-devtools.
I am running this after the webpage has loaded, but it should all be there still since its in the dev-tools?
What am I missing to get the full HTML out of a navigated site. I suspect this has to do with frames but after an hour of googling and reading old messages I see this only tangentially mentioned.
Winforms
package id="cef.redist.x64" version="3.2526.1362" targetFramework="net46"
package id="cef.redist.x86" version="3.2526.1362" targetFramework="net46"
package id="CefSharp.Common" version="47.0.3" targetFramework="net46"
package id="CefSharp.WinForms" version="47.0.3" targetFramework="net46"
I was having the same issue trying to get click on and item located in a frame and not on the main frame. Using the example in your answer, I wrote the following extension method:
public static IFrame GetFrame(this ChromiumWebBrowser browser, string FrameName)
{
IFrame frame = null;
var identifiers = browser.GetBrowser().GetFrameIdentifiers();
foreach (var i in identifiers)
{
frame = browser.GetBrowser().GetFrame(i);
if (frame.Name == FrameName)
return frame;
}
return null;
}
If you have a "using" on your form for the module that contains this method you can do something like:
var frame = browser.GetFrame("nameofframe");
if (frame != null)
frame.EvaluateScriptAsync("document.getElementById('whateveridyouwanttoclick').click();");
Of course you need to make sure the page load is complete before using this, but I plan to use it a lot. Hope it helps!
Thanks, some examples from previous versions had me confused how this works. I was looking for something like this.
var frameIdent = Browser.GetBrowser().GetFrameIdentifiers();
var result = Browser.GetBrowser().GetFrame(frameIdent.Last()).GetSourceAsync().Result;
textBox1.Text = result.ToString();
So I guess the way to get all HTML from a site is loop through the frames identifiers list, get the result from each frame via GetSourceAsync and concatenate them all to a string.

How to auto load URL through SWF banner in the background?

I am new here just signed up and I did not find information on how to classify this topic on specific section.
I am using adobe flash professional to create a small banner to auto load one of my own URL for analytics. Is this possible? I found one solution on another user's topic which was (AS 3):
var req:URLRequest("http://your_request");
var loader:URLLoader = new URLLoader ();
loader.addEventListener(Event.COMPLETE, onLoadComplete);
loader.load(req);
private function onLoadComplete(ev:Event):void
{
var result:String = ev.target.data;
}
But this doesn't seem to work. How can I test that the full webpage is loaded 'invisibly' in the background? If "http://your_request" has cookies for example, I can check if the cookies were created, but I do not know if with this method I'll load the cookies to check if this is working at the first place.
There is no way to get http-cookies natively. But you can do this with a js-helper-function and an ExternalInterface.call. See this answer: How do I access cookies within Flash?

Browsers cut off query strings sent from Flash

I need to send a query string from Flash so that it appears in address bar of a browser. Basically, I need to make a Flash version of this HTML code:
link
Seems simple... When I try to do this from Flash the browser cuts off the query string. So instead of getting Portfolio.htm?image=4, I only get Portfolio.htm This is the actionscript I am using:
var url:String = "Portfolio.htm";
var variables:URLVariables = new URLVariables();
variables.image = '4';
var newRequest:URLRequest = new URLRequest(url);
newRequest.data = variables;
newRequest.method = URLRequestMethod.POST;
navigateToURL(newRequest);
This actionscript works great when I click the button from the Flash player. It stops working when I try to put the swf into a HTML wrapper.
The query string image=4 is not going to PHP file. Portfolio.htm uses Javascript to strip the variable from the URL. Most of the questions surrounding this topic deal with PHP files and don't care if the query string appears in the browser address bar.
Try changing the URLRequestMethod to GET instead of POST.
newRequest.method = URLRequestMethod.GET;
Or simply leave it out because GET is the default method.

Save a html canvas image

I know there is probably an answer already to this question but I haven't been able to find it yet and there is a deadline on my project.
So I have made an html5 canvas and I would like to be able to do two things with one(or more) buttons.
I would like the user to be able to save what he has just done by clicking on the save button and ideally I would like the image to be downloaded (as opposed to having to right click and "Save image as". This is what I have been able to do so far).
I would also like the image to be saved (maybe to a database? or a server? I don't know how it works) so that a part of the drawing (or all of it, depending on the difficulty of the code) that has been done before is used the next time someone else logs on (not necessarily the same person with the same IP). Is this possible?
I am very new to code and self teaching myself so any extra comments on the code to help me understand would be extra-appreciated.
Regarding the first task, you can export the canvas content to an image with toDataUrl method supported by the canvas object.
var canvas = document.getElementById("canvas");
if (canvas.getContext) {
var ctx = canvas.getContext("2d"); // Get the context for the canvas.
var myImage = canvas.toDataURL("image/png"); // Get the data as an image.
}
var image = document.getElementById("image"); // Get the image object.
image.src = myImage;
As regarding the second task, after you saved the canvas to an image you can upload the resulted image into the database by using an ajax call. Here is a simple example for how to use it:
$.ajax({
url: "upload.php",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res) {
document.getElementById("response").innerHTML = res;
}
});
For a full example see these articles:
http://net.tutsplus.com/tutorials/javascript-ajax/uploading-files-with-ajax/
http://coursesweb.net/ajax/upload-images
You would probably need to use JavaScript and maybe PHP. I am not proficient in both of those so I can't help you, but you should look up tutorials on how to make a database on your website.
i think you might be interested to have a look on the html5 file api :
http://updates.html5rocks.com/2012/08/Integrating-input-type-file-with-the-Filesystem-API
It would allow you to deal with your issue with no server-side code.
(Rq : this will not solve all your issues.)