I am using Rhino to evaluate some javascript, where I simply pass a ByteArray from kotlin to a function in Javascript. I know it is in bad taste to say this but I am using the same Js file in Swift, and in .Net Core, without an issue with the line that is failing in this case.
As below, I am passing bytes, a ByteArray, to the JS function decodeByteArray(). The line that fails is the one I have marked with the comment //invalid argument
I have checked the ByteArray contents and they are as expected.
Am I doing something wrong or missing something in this?
Javascript
function decodeByteArray(buf) {
var pbf = new Pbf(buf);
return JSON.stringify(decode(pbf));
}
function Pbf(buf) {
this.buf = ArrayBuffer.isView && ArrayBuffer.isView(buf) ? buf : new Uint8Array(buf);
this.pos = 0;
this.type = 0;
this.length = this.buf.length;
setUp(this);
}
Kotlin
private fun decodePbfBytes(bytes: ByteArray?): Any? {
var jsResult: Any? = null;
var params = arrayOf(bytes)
val rhino = org.mozilla.javascript.Context.enter()
rhino.optimizationLevel = -1
rhino.languageVersion = org.mozilla.javascript.Context.VERSION_ES6
try{
val scope = rhino.initStandardObjects()
val assetManager = MyApp.sharedInstance.assets
val input = assetManager.open("pbfIndex.js") //the js file containing js code
val targetReader = InputStreamReader(input)
rhino.evaluateReader(scope, targetReader, "JavaScript", 1,null)
val obj = scope.get("decodeByteArray", scope)
if (obj is org.mozilla.javascript.Function){
jsResult = obj.call(rhino, scope, scope, params)
jsResult = org.mozilla.javascript.Context.toString(jsResult)
}
}catch (ex: Exception){
Log.e("Error", ex.localizedMessage)
}finally {
org.mozilla.javascript.Context.exit()
}
return jsResult
}
Rhino's TypedArray support is a little lacking (see https://mozilla.github.io/rhino/compat/engines.html#ES2015-built-ins-typed-arrays)
I couldn't get the constructor to take a byte[] directly, but it worked after converting to a javascript array.
I believe it will work to change new Uint8Array(buf); to new Uint8Array(Array.from(buf));
While Uint8Array.from is not implemented, Array.from is.
Related
I am trying to upload a file along with some metadata to a WebApi Service that I have created with ng-file-upload and Angular. I am getting the file name and bytes as expected, but I am not able to get the metadata I am passing as well. Here is what I am doing on the Angular side
Upload.upload({
url: '/api/FileStorage/AddContent' + location.search,
data: {file: files, year: vm.year }
})
And the WebApi side
var streamProvider = new CustomMultipartFileStreamProvider();
IEnumerable<HttpContent> parts = null;
Task.Factory
.StartNew(() => parts = Request.Content.ReadAsMultipartAsync(streamProvider).Result.Contents,
CancellationToken.None,
TaskCreationOptions.LongRunning, // guarantees separate thread
TaskScheduler.Default)
.Wait();
var customData = streamProvider.CustomData;
Here I am using a MultiStreamProvider to get the file, here is the meat of that provider
public override Task ExecutePostProcessingAsync()
{
foreach (var file in Contents)
{
var parameters = file.Headers.ContentDisposition.Parameters;
var filename = GetNameHeaderValue(parameters, "filename");
var year = GetNameHeaderValue(parameters, "year");
}
return base.ExecutePostProcessingAsync();
}
I am able to get filename without issue, but am never able to get the year. Here is the value in the debugger when I am looking at the parameters variable
As you can see, the name is "name" and the value is "year" when I would expect the name to be "year" and value to be "2016" or whatever I am passing in. What am I doing wrong here and how do I get the metadata included in the same call to the Api?
We use a similar approach with ng-file-upload and WebAPI. To get the values out of the form data, we weren't able to use GetNameHeaderValue. We had to do some manual parsing. We decided to use modified version of what was posted at http://conficient.wordpress.com/2013/07/22/async-file-uploads-with-mvc-webapi-and-bootstrap/ to dynamically take a form and unload it to a strongly-typed Model. But basically, here's what it does in the ExecutePostProcessingAsync method:
public override async Task ExecutePostProcessingAsync()
{
var formData = new FormCollection();
for (int index = 0; index < Contents.Count; index++)
{
ContentDispositionHeaderValue contentDisposition = headers.ContentDisposition;
if (contentDisposition != null)
{
HttpContent formContent = Contents[index];
string formFieldName = UnquoteToken(contentDisposition.Name) ?? String.Empty;
// Read the contents as string data and add to form data
string formFieldValue = await formContent.ReadAsStringAsync();
formData.Add(formFieldName, formFieldValue);
}
//For your case
var filename = formData["filename"];
var year = formData["year"];
This is the UnquoteToken method this uses:
private static string UnquoteToken(string token)
{
if (String.IsNullOrWhiteSpace(token))
{
return token;
}
if (token.StartsWith("\"", StringComparison.Ordinal) && token.EndsWith("\"", StringComparison.Ordinal) && token.Length > 1)
{
return token.Substring(1, token.Length - 2);
}
return token;
}
is it possible to render a specific page in a razor function. I tried #RenderPage but i cant figure out the path. Are there any built in functions to accomplish this?
Thanks Johan
Not really a C1 specific approach, but personally my best approach has been to just make a separate web-request for the page in question, parse out the html and render it.
This code can serve as an example, its a 1:1 of what i'm using. As you can see the trick is to find the element that wraps your content, in my example its the element inside that has an id equals to ContentColumnInner
public static string GetContentFromPage(Guid pageId)
{
var DomainName = HttpContext.Current.Request.Url.Authority;
var Uri = String.Format("http://{0}/page({1})", DomainName, pageId);
var request = WebRequest.Create(Uri);
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
using (var response = (HttpWebResponse)request.GetResponseWithoutException())
{
if (response.StatusCode != HttpStatusCode.OK)
{
LogError("StatusCode: " + response.StatusCode);
return null;
}
// Get the stream containing content returned by the server.
using (var responseStream = response.GetResponseStream())
{
if (responseStream == null)
{
LogError("ResponseStream is null");
return null;
}
// Open the stream using a StreamReader for easy access.
using (var stream = new StreamReader(responseStream))
{
// Read the content.
var responseFromServer = stream.ReadToEnd();
var beforeBodyStartIndex = responseFromServer.IndexOf("<body", StringComparison.Ordinal);
var afterBodyEndIndex = responseFromServer.LastIndexOf("</body>", StringComparison.Ordinal) + 7;
var body = responseFromServer.Substring(beforeBodyStartIndex, afterBodyEndIndex - beforeBodyStartIndex);
try
{
var xmlDocument = XElement.Parse(body);
var content = xmlDocument.Descendants().FirstOrDefault(o => o.Attribute("id") != null && o.Attribute("id").Value.EndsWith("ContentColumnInner"));
if (content == null || !content.HasElements)
{
return null;
}
var reader = content.CreateReader();
reader.MoveToContent();
return reader.ReadInnerXml();
}
catch (XmlException ex)
{
LogError("Error parsing xml: " + ex.Message);
return null;
}
}
}
}
}
I have a problem with ActionScript (AS3) / JSON. There is probably a really simple answer to this, so I apologize if this seems like a really noob question.
What I am trying to do is take multiple text strings, place them into an object and then have that object sent as a JSON string to a server.
Every thing else in my code works fine, accept whenever I input a piece of text into my text input box and send the object, the array produces null entries like so:
{"email":null,"last_Name":null,"first_Name":null,"date_of_birth":null,"telephone":null}
However if I replace customer.first_name = inPutfname with: customer.first_name = " John";
Then the array traces back as the following which is what I want:
{"email":null,"last_Name":null,"first_Name":John,"date_of_birth":null,"telephone":null}
So how do I take the multiple input text strings and place them into the object so that the array reads back as it does in the above example, or ideally like this example:
{"email":randomemail#email.com"last_Name":smith,"first_Name":john,"date_of_birth":27/07/1989,"telephone":012343456788}
Here is the code I have so far:
var inPutFirstname: String;
var inPutLastname: String;
var inPutEmail: String;
var inPutTelephone: String;
var inPutDob: String;
// changes customer data into an object
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
var customer:Object = new Object;
customer.first_Name =inPutFirstname;
customer.last_Name = inPutLastname; PROBLEM AREA?
customer.email = inPutEmail;
customer.date_of_birth = inPutDob;
customer.telephone = inPutTelephone;
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//changes customer object into json string
var myJson: String = JSON.stringify(customer);
var myVariableUrl: URLVariables = new URLVariables();
myVariableUrl.data = JSON.stringify(myVariableUrl);
var myRequestUrl: URLRequest = new URLRequest();
var authHeaderUrl: URLRequestHeader = new URLRequestHeader("xxxxxx", "xxxxxxx");
myRequestUrl.method = URLRequestMethod.POST;
myRequestUrl.data = myJson;
myRequestUrl.url = "website url"
var uload: URLLoader = new URLLoader();
uload.load(myRequestUrl);
uload.load(myRequestUrl);
mySendbutton.addEventListener(MouseEvent.MOUSE_DOWN, thefunction1);
// captures text from the input and places them into the customer object
function captureText1(): void {
inPutFirstname = myFirstName.text;
inPutLastname = myLastName.text;
inPutEmail = myEmail.text;
inPutDob = myDob1.text + myDob2.text + myDob3.text;
inPutTelephone = myTele.text;
}
function thefunction1(event: MouseEvent): void {
captureText1();
trace(inPutfname);
trace(myJson);
}
Any Help would be Amazing.
Thanks guys
Your problem is that the code for creating the JSON object doesn't seem to be contained in a function, which means it will be executed immediately (considering this is probably code attached to a frame).
What you need to do is to is to wrap the code relating to creating the JSON object into a function, something like this:
(Please note that there are a lot of ways this code can be structured better, however this should illustrate some core concepts.)
var inPutFirstname: String;
var inPutLastname: String;
var inPutEmail: String;
var inPutTelephone: String;
var inPutDob: String;
function generateJSON():String {
var customer:Object = new Object();
customer.first_Name = inPutFirstname;
customer.last_Name = inPutLastname;
customer.email = inPutEmail;
customer.date_of_birth = inPutDob;
customer.telephone = inPutTelephone;
return JSON.stringify(customer);
}
function sendJSON(data)
var myVariableUrl: URLVariables = new URLVariables();
myVariableUrl.data = JSON.stringify(myVariableUrl);
var myRequestUrl: URLRequest = new URLRequest();
var authHeaderUrl: URLRequestHeader = new URLRequestHeader("xxxxxx", "xxxxxxx");
myRequestUrl.method = URLRequestMethod.POST;
myRequestUrl.data = data;
myRequestUrl.url = "website url"
var uload: URLLoader = new URLLoader();
uload.load(myRequestUrl);
}
mySendbutton.addEventListener(MouseEvent.MOUSE_DOWN, thefunction1);
// captures text from the input and places them into the customer object
function captureText1(): void {
inPutFirstname = myFirstName.text;
inPutLastname = myLastName.text;
inPutEmail = myEmail.text;
inPutDob = myDob1.text + myDob2.text + myDob3.text;
inPutTelephone = myTele.text;
}
function thefunction1(event: MouseEvent): void {
// First populate the string variables with user input
captureText1();
// Then generate the JSON string
var jsonData:String = generateJSON();
// Lastly, send the JSON string
sendJSON(jsonData);
trace(inPutfname);
trace(jsonData);
}
1st of all, can you add in function captureText1() traces for:
trace(myFirstName.text);
trace(myLastName.text);
trace(myEmail.text);
etc...
just to be sure that you have proper value in there.
And if you are, instead of using extra variables for that you can do (right after traces):
var customer:Object = new Object;
customer.first_Name = myFirstName.text;
customer.last_Name = myLastName.text;
customer.email = myEmail.text;
customer.date_of_birth = "" + myDob1.text + myDob2.text + myDob3.text;
customer.telephone = myTele.text;
var myJson: String = JSON.stringify(customer);
trace(myJson);
I'm looking for a way to store variables of same type in an array, and access them by their names. Like in PHP you do:
$array = array();
$array['first member']=$var1;
$array['another member']=$var2;
// so on
I want to do a similar thing in Unity3D's JavaScript. I have AudioClip variables, currently used like this:
#pragma strict
var Audio_Goal:AudioClip;
var Audio_Miss:AudioClip;
var Audio_Saved:AudioClip;
function PlaySound_Goal()
{
audio.PlayOneShot(Audio_Goal);
}
function PlaySound_Miss()
{
audio.PlayOneShot(Audio_Miss);
}
of course this requires one function per audioclip, which is ugly.
I want to do it like this:
function PlayAudio(audioname:String)
{
audio.PlayOneShot(AudioArray[audioname]);
}
How can I do this?
You need to use something like a dictionary:
#pragma strict
import System.Collections.Generic;
var AudioArray = new Dictionary.<string,AudioClip >();
// add to dict
AudioArray["one"] = AudioClip.Create(/**/); //see AudioClip.Create(...) for arguments
// play from dict
audio.clip = AudioArray["one"];
audio.Play();
You can do something like:
var myAudioClips: AudioClip[];
function Start() {
// Your stuff...
}
function PlayAudio(audioname:String)
{
for (var myClip in myAudioClips) {
if (audioname.Equals(myClip.name)) {
audio.PlayOneShot(myClip);
}
}
}
And then add all of your clips in the array by dragging them using the editor.
You should be able to use Enumerable.Zip>, but because it's not available in UnityScript, I think this is the best you can do:
var names : List.<String>;
var clips : List.<AudioClip>;
var keyFunc = function(kvp : KeyValuePair.<String, AudioClip>) kvp.Key;
var valueFunc = function(kvp : KeyValuePair.<String, AudioClip>) kvp.Value;
var dictionary = names.Select(
function(name, index) new KeyValuePair.<String, AudioClip>(name, clips[index])
).ToDictionary(keyFunc, valueFunc);
I don't know why you need to store the Funcs. This code compiles, but won't run:
var dictionary = names.Select(
function(name, index) new KeyValuePair.<String, AudioClip>(name, clips[index])
).ToDictionary(function(kvp) kvp.Key, function(kvp) kvp.Value);
Report it as a bug if you want. I don't want to, because I don't think the devs need to waste any more time on this language.
I'm trying to get my SWF loader to work with an array so I can call my swf files via one code, using buttons.
This is the problem I am getting:
Scene 1, Layer 'actions', Frame 2, Line 68 1067: Implicit coercion of a value of type Array to an unrelated type String.
I am not too good with arrays, or strings, or coding tbh, i'm not too sure what the problem is, I understand it, my array and my string don't work together,basically, but I don't know how to fix it, if it can be fixed/work with the code I am using.
just some help and being pointed in the right direction would be a treat
var swfList:Array = ["imagegallery.swf", "videoplayer.swf"];
var SWFLoader = new Loader;
var SWFRequest = new URLRequest (swfList) ;
SWFLoader.load (SWFRequest) ;
function loadSWF(file:String, container:MovieClip=null):void
{
if(container == null) container = MovieClip(root);
if(SWFLoader != null)
{
if(SWFLoader.parent) SWFLoader.parent.removeChild(SWFLoader);
}
addChild (SWFLoader);
}
vidPlayer_btn.addEventListener (MouseEvent.CLICK, goVidPlayer);
function goVidPlayer (e:MouseEvent):void
{
loadSWF("videoplayer.swf");
}
imageGallery_btn.addEventListener(MouseEvent.CLICK, goImageGallery);
function goImageGallery(e:MouseEvent):void
{
loadSWF("imagegallery.swf");
}
To access items within an array use this format:
var SWFRequest = new URLRequest(swfList[i]);
Where i is the position in array (starting at zero).
For instance:
var SWFRequest = new URLRequest(swfList[0]);
gives the same result as:
var SWFRequest = new URLRequest("imagegallery.swf");
Did away with the array but still "have one code for both buttons instead of two separate codes".
// Looks unnecessary
// var swfList:Array = ["imagegallery.swf", "videoplayer.swf"];
// Transfer inside loadSWF()
// var SWFRequest = new URLRequest (swfList); // needs a url String parameter
// SWFLoader.load (SWFRequest);
var SWFLoader = new Loader(); // Don't forget the parenthesis
vidPlayer_btn.addEventListener (MouseEvent.CLICK, goVidPlayer);
imageGallery_btn.addEventListener(MouseEvent.CLICK, goImageGallery);
function goVidPlayer (e:MouseEvent):void
{
loadSWF("videoplayer.swf");
}
function goImageGallery(e:MouseEvent):void
{
loadSWF("imagegallery.swf");
}
function loadSWF(file:String, container:MovieClip=null):void
{
// What for?
// if(container == null) container = MovieClip(root);
if(SWFLoader != null)
if(SWFLoader.parent)
SWFLoader.parent.removeChild(SWFLoader);
var SWFRequest = new URLRequest (file) ;
SWFLoader.load (SWFRequest);
addChild (SWFLoader);
}