Restricting file types in AngularJS with HTML input type="file" - angularjs

So my HTML passes a file into AngularJS, and it is named as myFile.
So I use:
console.log($scope.myFile.type);
and it prints out 'application/pdf'.
But when I use this line:
if ($scope.myFile.type == 'application/pdf'){
// some stuff here
}
or
($scope.myFile.type == {'image/jpeg': fileMimeType})
Those will not ever be equal to true. I have no idea how to run this comparison anymore, and would appreciate snippets that would allow me to somehow create this comparison.
Thanks.

I wish i could make comments, but alas i do not have enough points. Did you try console.log( typeof $scope.myFile.type); to make sure it was a string? Also did the output itself have single quotes in it? because if it does have the quotes in it you'll either need to strip those or compare with the quotes in your string.

Related

LogicApp Split and Replace having problems with \n

I have been trying to split a string into an array of each line \n
As this doesn't work I tried replacing replace(outputs('Compose_6'),'\r\n','#') with a view to then splitting on #.
I have searched the internet and tried various things but nothing seems to work.
Can someone explain how to do this?
Thanks in advance
Using split(variables('string var'),'\n') expression, you can split string into array. By default logic app will add an extra black slash to original back slash. So suggesting you to change expression in code view as mentioned above.
I have created logic app as shown below,
In first initialize variable action, taken a string variable with text as shown below
Hello
Test split functionality
Using logic apps
Next initialize variable action, using a array variable and assigning value using expression as split(variables('string var'),'\n'). Make sure you dont have double back slash added in code view. Only one back slash should be there when you see it in code view.
Code view:
The output of logic app can be shown below,
Refer this SO thread.

1 liner regex to get everything before semi colon

I have this variable 1874;#Bob Williams. I tried this and it should give me 1874 but it's giving me the entire variable. Any idea?
(?<=\w+;)
Whatever tool/engine you're using is removing what it matches and returns what remains after removal, so this should work for you: ;.+
A bit off topic but you dont need a regex for this. Not at all. Use Substringinstead:
var s = "1874;#Bob Williams";
s = s.Substring(0, s.IndexOf(';')); // If your input might not contain a semi colon, check the return of IndexOf
// s == "1874"
Demo
If you are writing .NET code as your tag show, use this.

Pass regex to variable (angular)

I have such a code ng-init="validationRegex = '#RegularExpression.expression'" where RegularExpression.expression is c# string variable = "(\w+\/|\w+\\)+(\w+)\.\w+". I want to pass variable value to angular controller using ng-init. But in the end I get (w+/|w+\)+(w+).w+. How can I get right value?
I assume that your C# code is actually: #"(\w+\/|\w+\\)+(\w+)\.\w+". The # avoids needing the evil escaped escape.
You will likely need to use the evil escaped escape in this case. Note that you don't need to escape the / if you are putting it directly in a string (if you are using it how I think you are). You can also use braces to minimize the escaping. So you can take this regex:
(\w+/|\w+\\)+(\w+)[.]\w+
and pass it through regex planet to get this:
"(\\w+/|\\w+\\\\)+(\\w+)[.]\\w+"
I would not recommend manual conversion since you already have an escaped backslash.
Double escape the string using:
#RegularExpression.expression.Replace(#"\", #"\\")
When it comes through on the JavaScript side it will be singly-escaped again.

In XDocReport, how to handle null value?

Is there a way to handle null value for a field in XDocReport? or do I need to manipulate it on my own? example:
if (thisVar == null)
context.put("sampleText", "");
else
context.put("sampleText", thisVar);
or is there an option in docx quick parts?
I found this line in the error message of XDocReport. However I could not understand where to apply this, in the template or in the code.
Tip: If the failing expression is known to be legally refer to
something that's sometimes null or missing, either specify a default
value like myOptionalVar!myDefault, or use [#if
myOptionalVar??]when-present[#else]when-missing[/#if]. (These only
cover the last step of the expression; to cover the whole expression,
use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
In docx, append ?if_exists to the field name
«${tx.amount?if_exists}»
you may also append !
«${tx.amount!}»
Please refer to this link for those who uses freemarker. How to check if a variable exists in a FreeMarker template?

unterminated string literal error in salesforce

I am trying to get a value from salesforce class into a javascript variable. I am able to get the value if its a single line value, but if its multiline textarea it gives a unterminated string literal error
caseUpdate.Description = "{!ac__c.C_Info__c}";
After googling for sometime i came to know we can have a workaround for this by having a hidden field and using DOM storing it using the document.getElement.Id. But i am calling this code on a button click so i would not be able to create a input text or hidden field value.
Any body who can provide an way to do it?
Thanks
Prady
You should just be able to use the standard Salesforce JSENCODE() function if you are using OnClick Javascript in a button. This will escape any characters for you.
See the documentation.
It is because of line breaks. merge fields are rendered unescaped into the output stream meaning that CRLFs push into a new line and break javascript strings. Either use the div/input trick or use Apex to replace \r\n's in the field with <br/> or whatever best suits the purpose. Also keep in mind that " will also terminate your JS string.
The easiest way is to just include a function in your extension and then you can use it across board
public String FixNewLine(String s) {
if (s != null) return s.replaceAll('\r\n', '<br/>').replaceAll('"', '\\"');
return null;
}
I had the same issue but was able to fix it! The trick is the JSENCODE function. Its basically {!JSENCODE(Obj.Field)}"; So you are replacing your merge field with this function and nesting the merge field itself within the function. In my scenario I ended up with opptyObj.Description="{!JSENCODE(Case.Description)}"; as my total syntax. First calling upon my set object and field, and then the merge data to populate it.

Resources