[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1996
  • Last Modified:

Convert PHP convert base64 to C#

Hi, I have the following in PHP that I am trying to convert to C#.

<?php
$dataurl = str_replace(" ", "+", $_POST["u"]);
$data = substr($dataurl, strpos($dataurl, ","));
$file_name = "saved/" . md5(uniqid(rand(), true)) . ".png";
$file = fopen($file_name, "wb");
fwrite($file, base64_decode($data));
fclose($file);
?>
<img src="<?php echo $file_name ?>" align="left" width="400" />

Open in new window


It takes an image that is encoded base 64 and converts to an image file which it then writes to the server. My problem mainly is the string replace at the start of the php code

Any help or pointers please?

Thanks
NL
0
nedlogan
Asked:
nedlogan
  • 10
  • 9
1 Solution
 
saraganiCommented:
Hi, the first line will take the POST data of the valiable "u", and replace the space with "+".
I don't if you know anything about HTTP protocol and GET / POST, but variables can be passed by ether GET (appears on address bar) or POST (On data that is being sent by the browser), where the format is like:

http://www.something.com/u=123&a=456&t=789

I have here 3 variables u, a, t.
(This is in the address, so those are GET variables).

The same applies to POST.
0
 
nedloganAuthor Commented:
Sorry I may not have been clear I understand the difference between GET and POST and that space is being replaced by "+". My problem is converting all this to C#.

Thanks anyway for your comment.
0
 
saraganiCommented:
Is it a desktop application or Asp.net?
Where do you get the base 64 stream from?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nedloganAuthor Commented:
Comes from javascript, going into ASP.NET.

Thanks
0
 
saraganiCommented:
Can't you use the NameValueCollection  to get the value of the POST on the asp.net side?

something like:
NameValueCollection nameValueCollection = Request.Form;
string dataurl;
if (!string.IsNullOrEmpty(nvc["u"]))
{
  dataurl = nvc["u"];
}
0
 
nedloganAuthor Commented:
I'm using ASP.NET WebPages Razor Syntax.

I tried something like this:

var dataurl = Request["u"];

byte[] ba = System.Text.Encoding.UTF8.GetBytes(dataurl);
var aba = Convert.ToString(ba);
var newfile = Convert.FromBase64String(aba);

//save newfile


But always gives the error:

"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters."

The fact that it works in PHP means the string is OK I just don't know how to format it.

Thanks
0
 
saraganiCommented:
Can you post the dataurl string?

Maybe you don't need to take the dataurl bytes as UTF8 and convert it to string.

Are you sure that Convert.ToString(ba);  actually converts your bytes array to a string?

Can you post the aba string?  (I'm pretty sure it is wrong).
0
 
saraganiCommented:
I was right...

Convert.ToString(ba) returns:
"System.Byte[]"
0
 
nedloganAuthor Commented:
It's 20000 characters! Do you want me to attach or just post a part of it?

Also, I've tried things like:
var base64string = Convert.FromBase64String(dataurl);

But error always as above.
0
 
saraganiCommented:
Ok, can you please take the dataurl string, paste it to a text file and attach it here?
0
 
saraganiCommented:
Can you also try?:

var dataurl = Request["u"];
var data = dataurl.Substring(dataurl.IndexOf(",") + 1);
var newfile = Convert.FromBase64String(data);
0
 
nedloganAuthor Commented:
The code you supplied in your last post gives the following as the converted URL

http://localhost:56963/save_sixtyfour/System.Byte[]

I've attached the base64.
base.txt
0
 
saraganiCommented:
Please test my last code. It works.
I was able to save and see the picture

this is while assuming that the dataurl is indeed a 196kb string
0
 
nedloganAuthor Commented:
Sorry still getting this:
http://localhost:56963/canvas_rotate/System.Byte[]

I copied your code directly:

@{
	if (IsPost){
	var dataurl = Request["u"];
    	var data = dataurl.Substring(dataurl.IndexOf(",") + 1);
    	var newfile = Convert.FromBase64String(data);
        <img src="@newfile" alt="Saved File" id="1savedfile" />
    }
}

Open in new window

0
 
saraganiCommented:
Off course it won't work!

Image source is expecting a file path. The fact that you managed to convert from a string to a byte array using Convert.FromBase64String without any exception, tells you that the conversion went fine.

Since you are giving it as a source a byte array then it does a ToString for the array, so you get System.Byte[] (plus the path of your code in your local server).


There are 3 options:
1) Take the byte array and save it to a temporary file and then have the src point to that temp file name.
(This is what the PHP code does. You can see that it is generating a unique name, save the bytes array into a file and then supplies that file to the image)

2) Supply the image source an ashx file like shown here:
http://stackoverflow.com/questions/1738020/bytearray-to-image-asp-net

3) work with a Picturebox instead of an <img> tag, create a Bitmap from the byte array and then set the Image property of the Picturebox to be the Bitmap you have just created. For example, look here:
http://forums.techarena.in/software-development/1196337.htm
0
 
nedloganAuthor Commented:
Excuse my ignorance! I'll take option 1. Thanks for your help. Points on the way.
0
 
nedloganAuthor Commented:
Thanks for your help.
0
 
saraganiCommented:
Although option number 1 seem to be the best, it requires writing to drive which is slower than accessing the ram. (In this case it is probably not noticeable)

Plus, you need to make sure to clear the temp files every once in a while, else you will run out of space, so make sure you do that. :-)
0
 
nedloganAuthor Commented:
Thanks again.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 10
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now