简体   繁体   中英

URL Encoded Base64 Encoded File Data

I'm trying to encode an image url to Base64 in ASP.NET so that that an open source API can process my request. The support team on the open source end of things sent me this code from their PHP system. I'm having problem translating that into a C# equivalent.

PHP Code:

$data = urlencode(base64_encode(fread(fopen("/tmp/signature.jpg", "r"),      filesize("/tmp/signature.jpg")))); 

Things I have tried so far:

string data = "http://url.com/image.jpg"
byte[] encbuff = HttpUtility.UrlEncodeToBytes(data);
string enc = Convert.ToBase64String(encbuff);
string urlenc = HttpUtility.UrlEncode(data);

Any help would be appreciated, Thank you.

The PHP fopen and fread functions are opening a file and reading its bytes. You're only working with the URL. You need to download it, or open if it's a local file, first:

byte[] data;
using (var webClient = new WebClient())
    data = webClient.DownloadData("http://url.com/image.jpg");
string enc = Convert.ToBase64String(data);
string urlenc = HttpUtility.UrlEncode(enc);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM