Invalid length for a Base-64 char array Exception under Chrome - c#

I have to send images generated by a javascript function to the server.
In order to do it, I store the base64 string of the image in a hidden textbox.
myTextBox.value = 'imagedata';
It works well for small size files (1MB or less).
However, when I try to send large files, the server returns an "Invalid length for a Base-64 char array" error.
The weird part is that I get this error with Chrome but not with Internet Explorer 10.
When I check the value of the string with the debugger, it seems like it is truncated with Chrome.
What causes this problem ? Is there a workaround ?
Thank you.

You shouldn't be using a single textbox to hold that much content.
Textboxes can only hold a limited amount of content. Looks like you're hitting that limit in chrome.
If you really must store that much content, then you'll have to break the content across multiple textboxes.
The max size depends from browser to browser.
See here for more information

It could be because QueryString is returning space instead of + (it does that in Chrome, but not I.E.).
Solution:
Decrypt(Request.QueryString["myvar"].Replace(' ', '+'))

Related

Capture signature using HTML5 and save it as image to database

I am just starting to learn Jquery and working on asp.net webform app that will be used on a touchscreen device, and I need to capture user signature, basically user will sign and after hit save, I want their signature to be saved as an image to SQL server database so I can retrieve and display it later on a webpage.
I found this question: Capture Signature using HTML5 and iPad
and the jQuery plugin works great, exactly as I want.
Here the demo: http://szimek.github.io/signature_pad
Here the plug-in: https://github.com/szimek/signature_pad
So on the demo I see after hit "save" you will go to a new tab that display an image of your signed signature, and I noticed that the url is something like
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAApIAAAE+CAYAAAA+vvBuAAAgAElEQVR4Xu3df8i37V0X8I/l0GY5BzZdiWslKhnNUaBR62lBP0htjuiHfz0uROgPmQsH/hE8jiKKjLl/IoLa9k+NMOYyhCRamyM0WI9bUBst5kzbUNfjxBlmaLy389jO+3q......
What does this url mean?
what type of variable will be used to store this in database table (nvarchar, binary...)
3 (MAIN QUESTION). How do I get those data text in code behind C# button click event to store them to a string variable for other purposes. Can someone provide a simple example so i can go from there?
if I am missing something please let me know, as I am looking at the .html file and those .js files in the demo project of that plugin, I am lost.
That URL is in BASE64 format. You can use that long string of characters in the SRC attribute of an image tag and it will be displayed.
To get better performance, the recommended way of storing photos is to store the image on the disk (using some kind of server side language) and then save the name of the file in a database as a CHAR or TEXT.
Not quite sure. I've never used the library before.
That is a Base64 encoded image.
data: says that data is following instead of a URL
image/png; specifies the content "mimetype" the data should be served as
base64, indicates the encoding type of the data
As base64 only uses ASCII characters, a varchar(MAX) would be suitable for storage. No need for nvarchar. I normally store the base64 encoding only (the last part after the comma), and keep the mime-type (e.g. image/png) in a separate field.
There are many options in C#. If you store the base64 part separately, you simplify the code a bit.
Turn it into an image server-side using byte[] imageBytes = System.Convert.FromBase64String(base64data) and creating an image from the byte array and type.
Inject the image into a webpage <img src="#Html.Raw("data:"+ mimetype + "base64," + base64data)"/>
Notes:
As #anthony mentions, your would typically store images as files (or in Blob storage nowadays) and only record the filename/URI. This depends on quantity size & usage.
We have found it convenient, for certain projects requiring extra security, for base64 images to be stored as encoded & encrypted strings in a database.
From comments: To save to, place the string value into a hidden input and update its value. It will then get posted back like any other string. Our own signature plugin just hides the original <input type="text"> it is attached to and puts the value there.
In addition to TrueBlueAussie's answer:
The simplest way to get your string in CodeBehind is:
Declare a HiddenField using ASP and assign a Static ID to it. (Static ID is important since ASP normally assigns automatically generated IDs to each control, and this can get difficult to predict).
Shape your JavaScript function in a way that SignaturePad writes the output base64 image string into this HiddenField. You can for example, use a button "Verify" that calls the Export function of the SignaturePad.
After the string is written into the HiddenField, read it back on CodeBehind. For this, you can use an additional button, for example something like "Save".
There are of course other options which are much more secure/versatile/appropriate, but this might be a good starting point for you.

javascript code is working fine in Internet Explorer but not working in Firefox and Google chrome

The below javascript code is working fine in Internet Explorer but not working in Firefox and Google chrome
var prand=11211411110311410111511510511111068508491;
//This is the value of variable prand that i get after calculation
prand = parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
//After this calculation I'm getting the expected result in IE,but the value is coming different in Firefox and Chrome
I have sorted out some more information ...
On alerting parseInt(prand.substring(10, prand.length)) , it's showing
232332021465786650000 on IE and 3.114101115115105e+29 on Chrome
if you wish to use substring, make sure your original variable is a string. Chrome finds that your prand variable has no quotes, so it tries to convert it into the largest number. IE on the other hand gives up and uses it as a string instead. Try declaring your variable as a string:
var prand = "11211411110311410111511510511111068508491";
Even then, the value of 311410111511510511111068508491 is too big to fit into a single integer. You might want to rethink your logic.

Is it OK to return a string with the size around 2 MB from a C# method?

I have a situation where I need to return the MAX of 2MB sized string from a method.
I have seen some web services returning big XML contents as strings. What is the optimum size with which we should transact in value parameter passing and returning?
There is no technically correct answer to this because it's perfectly normal to pass any amount of characters as long as it's needed in your program. You need to evaluate if passing such a large string is valueable to your program. If it is, great. If it's not, find a way to shorten it or pass a file path or database identifier instead so the service can get the text from there itself.
Is it OK to return a string with the size around 2 MB from a C#
if it is local (intranet,network) then it is mostly ok (unless the network is v v old). But, if you are passing it over the internet then you should consider the bandwidth of the client on which the data will be sent/received. Will it slow down your page or not etc. In most cases though it will be fine.
What is the optimum size with which we should transact in value parameter passing and returning
Any size that will not cause your application to slow down is fine.
It is completely okay, the only thing you should keep in mind is:
If a user is using 56kbps connection 2024/56=36.14 sec if this is not important for your case then no need to worry.

String Variable Character Limit

My String variable only stores 4096 characters, I need to store more, how can i achieve that?
Below is what i am trying to do
ServiceController[] myServices = ServiceController.GetServices();
String ServiceList = "";
foreach (ServiceController service in myServices)
{
ServiceList += service.DisplayName + "|||";
}
return ServiceList;
When the variable is returned, it only stores 4096 characters and rest are trimmed off.
P.S. I need them in one variable as I am making a URL out of them and passing to my webservice.
I need them in one variable as I am making a URL out of them and passing to my webservice.
No, don't do that!
A 4096 character URL is a very bad idea and is not guaranteed to work.
Extremely long URLs are usually a mistake. URLs over 2,000 characters will not work in the most popular web browser. Don't use them if you intend your site to work for the majority of Internet users.
(source)
Make a shortened URL that contains an id. Store the rest of the information in a database with the short id as the key.
Related
What is the maximum length of a URL in different browsers?
Maximum URL length is 2,083 characters in Internet Explorer
.NET string length limit is 2 billion characters.
Browsers do have a limit on how long of a URL they will accept, and the length limit is different across browser implementations. IE's limit is typically the shortest, at around 2k last time I checked in the IE6 era. Firefox and Chrome are considerably higher than that, but there is still a limit.
Your problem is elsewhere - but it is not possible to reliably use a URL that uses more than 2000 characters, you will need another approach entirely - see this SO answer: What is the maximum length of a URL?
(Also for building large strings use a StringBuilder instead)
Strings in C# has about 2Gig limit.so there is no problem with your string variable

H.225 User Information Packet Parsing

I'm writing some code using PacketDotNet and SharpPCap to parse H.225 packets for a VOIP phone system. I've been using Wireshark to look at the structure, but I'm stuck. I've been using This as a reference.
Most of the H.225 packets I see are user information type with an empty message body and the actual information apparently shows up as a list of NonStandardControls in Wireshark. I thought I'd just extract out these controls and parse them later, but I don't really know where they start.
In almost all cases, the items start at the 10th byte of the H.225 data. Each item appears to begin with the length which is recorded as 2 bytes. However, I am getting a packet that has items starting at the 11th byte.
The only difference I see in this packet is something in the message body supposedly called open type length which has a value of 1, whereas the rest all appear to be 0. Would the items start at 10 + open type length? Is there some document that explains what this open type length is for?
Thanks.
H.225 doesn't use a fixed length encoding, it user ASN.1 PER encoding (not BER).
You probably won't find a C# library. OPAL is adding a C API if you are able to use that.

Categories

Resources