c# how to write a jpg image from request.binaryread - c#

I have a flash app which sends raw data for a jpg image to a particular url Send.aspx . In Send.aspx I am using request.binaryread() to get the total request length and then read in the data to a byte array.
Then I am writing the data as jpg file to the server. The code is given below:
FileStream f = File.Create(Server.MapPath("~") + "/plugins/handwrite/uploads/" + filename);
byte[] data = Request.BinaryRead(Request.TotalBytes);
f.Write(data, 0, data.Length);
f.Close();
The file is getting created but there is no image in it. It always shows up as empty in any graphic viewer. What part am I missing. Am I supposed to use jpg encoding first before writing it to file? Thanks in advance

Well, you should use a using statement for your file stream, but other than that it looks okay to me.
A few suggestions for how to proceed...
Is it possible that the client isn't providing the data properly? Perhaps it's providing it as base64-encoded data?
Have you already read some data from the request body? (That could mess things up.)
I suggest you look closely at what you end up saving vs the original file:
Are they the same length? If not, which is longer?
If they're the same length, do their MD5 sums match?
If you look at both within a binary file editor, do they match at all? Any obvious differences?

Related

Convert Base64 String to an Image File and Save it to File System?

I am trying to make a .Net 6 console application that would take in a base64string and then save it to the file system as an actual image file
Example
I have this image
https://cdn.pixabay.com/photo/2016/03/28/12/35/cat-1285634_960_720.png
I would have this image already as a base64 string.
Now I want to save to my file system as "cat-1285634_960_720.png"
I just can't figure out how to do it. All the examples I see say to use Image.Save() but I can't find that in .Net6 and looks like it is removed.
First convert the base64 string to a byte array and then use File.WriteAllBytes(...) to save it:
byte[] imageByteArray = Convert.FromBase64String(base64String);
File.WriteAllBytes("image.png", imageByteArray);

Keep special characters in File When i save it

I have a file that contains some HTML code. I am trying to load this data into a C# Console app and transfer it into a JSON file to upload somewhere. When loading the file i am losing some of the encoding immediately when bringing the data in.
Example data
<li>Comfort Range: -60°F to 30°F / -50°C to -1°C</li>
Basic read file
//Load the file
String HTML_File = File.ReadAllText(location);
//Output the file to see the text
Console.WriteLine(HTML_File);
Console Output
<li>Comfort Range: -60??F to 30?F / -50?C to -1?C</li>
After i split the data how I need to, I than save the class to a JSON File
File.WriteAllText(OutputPath,JsonConvert.SerializeObject(HTMLDATA));
JSON file Data
<li>Comfort Range: -60�F to 30�F / -50�C to -1�C</li>
How can i go about loading this data and converting it to JSON without losing the encoding? I am still pretty new when it comes to encoding like this.
#JeremyLakeman helped me solve this, thank you sir!! When reading the text into the utility i needed to set the Encoding but not by the default ones.
File.WriteAllText(OutputPath,JsonConvert.SerializeObject(HTMLDATA), Encoding.GetEncoding("iso-8859-1"));
#JeremyLakeman helped me solve this, thank you sir!! When reading the text into the utility i needed to set the Encoding but not by the default ones.
File.WriteAllText(OutputPath,JsonConvert.SerializeObject(HTMLDATA), Encoding.GetEncoding("iso-8859-1"));

Azure Storage Blobs - Upload base64 as Text

I have an API server method that accept getting files as BASE64. Getting request like this:
{
file: "-BASE 64 HERE-"
}
I want that my server will get this file and store it on Azure Storage. So, I running this code:
var blob = container.GetBlockBlobReference("file.zip");
var buffer = Convert.FromBase64String(Model.File);
await blob.UploadFromByteArrayAsync(buffer, 0, buffer.Length);
It's working, but inefficient.
Why? Because the same bytes have two instances in my main memory: as byte array + as stream.
I wounder if it possible to upload the BASE64 as text, and letting the server understand that this is BASE64 - which will be treated like a file.
In that way I will upload the text directly without convert it to stream.
Is this possible?
May be linked to this thread
Thanks.
Can't you just use UploadFromStreamAsync?

File.ReadAllText does not return full content in C#

In my c# program, I have an image which is successfully stored in a byte[] data called bytes. I successfully write it into a .txt file using the following code
using (FileStream file = new FileStream("text.txt", FileMode.Create, FileAccess.Write))
{
file.Write(bytes, 0, numToWrite);
file.Close();
}
The above code stores the exact content I wish to store.
Whenever I wish to read the content of the file, text.txt, into textbox I only get the first line or little part of the first line. But when I open the file, text.txt, I see the complete content.
This is the code I use to read the file
string kk = File.ReadAllText("text.txt");
You have said at the start of the question that you have a byte[] that you are writing into the file. It's not clear why you decided not to use File.WriteAllBytes but let's assume that your code is correctly writing all the data into the file called "text.txt", which has been explained in comments does not magically make this a text file.
Using File.ReadAllText is not going to work because The data in the file is binary data, not text. As you can see from the remarks on the documentation, it will try to decide the encoding of the text file (which won't work because it contains binary data) and will do end of line processing which you won't want for a binary file.
The best way to read the data back is to use File.ReadAllBytes, which gives you back a byte[], just like you started with.

HttpHandler to render a png from an array of bytes

Okay, this should be simple, but I just can't get it to work. I have an array of bytes, read from a png file. I'm trying to write a (very) simple HttpHandler to render the image:
context.Response.AddHeader("Content-Type", "image/png")
context.Response.BinaryWrite(bytes)
context.Response.End()
When I open the page in a browser, I just get gibberish,
�PNG IHDR���X��sRGB���gAMA�� �a pHYs���o�d` ...
It's obviously something with the header information I'm doing wrong. Any suggestions?
Try using the ContentType property instead of AddHeader:
context.Response.ContentType = "image/png";
...
Two more diagnostics:
If you use "save" in the browser and save it to a png file, does that render properly?
Use Wireshark to see what's really coming back in the response (which exact bytes etc)

Categories