I have met this difficulty while decoding a Base64 encoded URL with parameters
eg: http://www.example.com/Movements.aspx?fno=hello&vol=Bits & Pieces
My expected results should be:
fno = hello
vol = Bits & Pieces
#Encoding:
//JAVASCRIPT
var base64 = $.base64.encode("&fno=hello&vol=Bits & Pieces");
window.location.replace("Movements.aspx?" + base64);
#Decoding c#
string decodedUrl = System.Text.Encoding.ASCII.GetString(Convert.FromBase64String(Request.Url.Query.Replace("?", ""))); // Replace is used to remove the ? part from the query string.
string fileno = HttpUtility.ParseQueryString(decodedUrl).Get("fno");
string vol = HttpUtility.ParseQueryString(decodedUrl).Get("vol");
Actual Result:
fno = hello
vol = Bits
I have searched stackoverlow and seems I need to add a custom algorithm to parse the decoded string. But as the actual URL is more complicated than shown in this example I taought better asks experts for an alternative solution!
Tks for reading!
Your querystring needs to be properly encoded. Base64 is not the correct way. Use encodeURIComponent instead. you should encode each value separately (although not needed in most parts in the example):
var qs = "&" + encodeURIComponent("fno") + "=" + encodeURIComponent("hello") + "&" + encodeURIComponent("vol") + "=" + encodeURIComponent("Bits & Pieces");
// Result: "&fno=hello&vol=Bits%20%26%20Pieces"
Then you don't need to Base64 decode in C#.
var qs = HttpUtility.ParseQueryString(Request.Url.Query.Replace("?", ""));
var fileno = qs.Get("fno");
var vol = sq.Get("vol");
If the URL were correctly encoded, you would have :
http://www.example.com/Movements.aspx?fno=hello&vol=Bits+%26+Pieces
%26 is the url encoded caract for &
and spaces will be replaced by +
In JS, use escape to encode correctly your url!
[EDIT]
Use encodeURIComponent instead of escape because like Sani Huttunen says, 'escape' is deprecated. Sorry!
Related
I have a string: <p><img title="\pi a{^{2}}" src="http://latex.codecogs.com/gif.latex?\pi&space;a{^{2}}" /></p> I want to replace it using base64 string.
Code:
string soalP = file.Path;
string decodeFile = Uri.UnescapeDataString(file.DisplayName);
byte[] imageArray = System.IO.File.ReadAllBytes(soalP);
string base64ImageRepresentation = Convert.ToBase64String(imageArray);
soal = Regex.Replace(soal, "\"http://latex.codecogs.com/gif.latex?" + "\\" + decodeFile + "\"", "data:image/jpeg;base64," + base64ImageRepresentation);
I have a error message:
parsing '"http://latex.codecogs.com/gif.latex?\pi&space;a{^{2}}"' - Malformed \p{X} character escape.
How to handle it?
Note:
I downloaded the picture first and saved it in the local folder.
The file name is encoded file name
Since you are using regular expressions, in the Regex.Replace() method, the second argument is treated as a regular expression for parsing. When there are special characters in the string, it may be mistaken as the symbol resolution of the regular expression, which leads to failure. If you are still confused about the backslash, you can use the string.Replace() method for character substitution.
soal = soal.Replace("\"http://latex.codecogs.com/gif.latex?" + "\\" + decodeFile + "\"", "data:image/jpeg;base64," + base64ImageRepresentation);
I have problem in opening file with ambersand in between.
var attachment = "attachment;" + "&test& incident&.txt";
HtmlPage.Window.CreateInstance("foo2", new object[] { Id.ToString(), attachment });
function foo2(theAlert, type) {
alert(type);
window.open('fileViewer.aspx?Id=' + theAlert + '&Type=' + type);
}
When i try to get the type in another page i am getting only the "attachment;" because it taking the words before ampersand. and missing my filename. If i give file name without ampersand i dont have any problem in opening the file.
Any one help me please?
Thanks in advance.
You can use encodeURIComponent
The encodeURIComponent() function encodes a URI component.
This function encodes special characters. In addition, it encodes the
following characters: , / ? : # & = + $ #
window.open('fileViewer.aspx?Id=' + theAlert + '&Type=' + encodeURIComponent(type));
Try the following:
window.open("fileViewer.aspx?Id=" + theAlert + "&Type=" + encodeURIComponent(type));
Changed from encodeURI to encodeURIComponent:
encodeURI is intended for use on the full URI.
encodeURIComponent is intended to be used on .. well .. URI components that is any part that lies between separators (; / ? : # & = + $ , #).
I need to pass a URL from C# to javascript. The problem is if the filename has single quote, it does not execute the javascript. When I use HttpUtility.HtmlEncode(fileNameWithoutEx) it does execute javascript but if filename is say "Copy of David's Birth Certificate" then URL gets converted to ?View.aspx?length=60&ext=pdf&file=Copy of David' Birth Certificate.
When View.aspx tries to get the querystring file i.e; filename, it gets set to "Copy of David" and not "Copy of David's Birth Certificate". Because of & it does not get the rest of the querystring.
if (System.IO.File.Exists(fileLocation)) {
string fileNameWithoutExt = System.IO.Path.GetFileNameWithoutExtension(fileLocation);
string fileExtension = System.IO.Path.GetExtension(fileLocation).Replace(".", "");
string title = System.IO.Path.GetFileNameWithoutExtension(fileName);
string url = "View?length=" + 60+ "&ext=" + fileExtension + "&file=" + fileNameWithoutExt;
ScriptManager.RegisterStartupScript(this, GetType(), "ShowPDF", "$(document).ready(function(){ShowPDFDocument('" + title + "', '" + url + "');});", true);
}
How can I send url with single quote to javascript?
What's the best way to handle ' and other special characters?
Use Uri.EscapeDataString(yourLinkHere);. See Uri.EscapeDataString on MSDN.
You're embedding the title and URL within quote-delimited strings in JavaScript, so you need to escape them.
title = title.Replace("'","\\'");
url = url.Replace("'","\\'");
You can try percent codes. I would recommned using %20 in the place of spaces as well. You can chance the file name itself or the way you route to it in code.
urlstring.replace("'", "%27")
Try to replace your ' with %27 which is the standard percent encode for '
Characters with reserved purposes should be replaced to guarantee functionality in all environments. You can view the list and read up on this here.
I need to encode a url in my WP7 application. The class im using dosent seem to encode # (to %23). Any ideas what im doing wrong?
string foo = InkBunnyUrls.Login + "&username=" + txtUsername.Text + "&password=" + txtPassword.Password;
//foo = https://inkbunny.net/api_login.php?output_mode=xml&username=test&password=foobar#1
string url = System.Net.HttpUtility.HtmlEncode(foo);
// url = https://inkbunny.net/api_login.php?output_mode=xml&username=test&password=foobar#1
Edit: I tried UrlEncode and that dosent work (see below). Reading the msdn doc it wont escape # . I cant use the system.web class as it isnt in WP7
string foo = InkBunnyUrls.Login + "&username=" + txtUsername.Text + "&password=" + txtPassword.Password;
//foo = https://inkbunny.net/api_login.php?output_mode=xml&username=test&password=foobar#1
string url = Uri.EscapeUriString(foo);
// https://inkbunny.net/api_login.php?output_mode=xml&username=test&password=foobar#1
The # symbol doesn't need to be HTML-encoded.
If you're expecting the result to be %23 then you should look at UrlEncode instead:
string encoded = HttpUtility.UrlEncode("#"); // "%23"
EDIT...
Do the WP7 libs support EscapeDataString?
string encoded = Uri.EscapeDataString("#"); // "%23"
I think you are looking for UrlEncode and not HtmlEncode.
I have a problem that if I pass a string that contain + in a query string and try to read it , it get the same string but by replacing + with empty char
For example if i pass query like ../Page.aspx?data=sdf1+sdf then in page load I read data by data = Request.QueryString["data"] it will get as below data ="sdf1 sdf"
I solve the problem by replacing any empty char with + ..
But Is there any problem that cause that ? and Is my solution by replacing empty char with + is the best solution in all cases ?
Because + is the url encoded representation of space " ". If you want to preseve the plus sign in your value you will need to url encode it:
"/Page.aspx?data=" + HttpUtility.UrlEncode("sdf1+sdf")
which will produce:
/Page.aspx?data=sdf1%2bsdf
Now when you read Request.QueryString["data"] you will get what you expect.