0

I have project where a user side framework written in PHP uses file_get_contents to read image files and place the output string into DB which then used to display the image to user by reading from DB.

Now i am making a webservice(webAPI in c#) by which user sends image in a byte[] from mobile app. and I need to find the exact alternate mathod for file_get_contents for C#.

Searched through out the web and found out two ways.

  1. byte[] fileData = File.ReadAllBytes(filePath); string sContents = System.Text.Encoding.ASCII.GetString(fileData);

  2. StreamReader sr = new StreamReader(filePath); string sContentsa = sr.ReadToEnd(); sr.Close();

from a sample image file the output for different methods are:

file_get_contents gives : {�PNG\r\n\n\0\0\0\rIHDR\0\0�\0\0�\b\0\0\0���(\0\0\0\tpHYs\0\0\v\0\0\v\0��\0\0\nOiCCPPhotoshop ICC profile\0\0xڝSgTS�}

First c# method: ?PNG\r\n\n\0\0\0\rIHDR\0\0?\0\0?\b\0\0\0???(\0\0\0\tpHYs\0\0\v\0\0\v\0??\0\0\nOiCCPPhotoshop ICC profile\0\0x?SgTS

second c# method: �PNG\r\n\n\0\0\0\rIHDR\0\0�\0\0�\b\0\0\0���(\0\0\0\tpHYs\0\0\v\0\0\v\0��\0\0\nOiCCPPhotoshop ICC profile\0\0xڝSgTS�

So looks like second c# method generates the same output, so here comes the problem part : while reading the value from DB the output shows like

"?PNG\r\n\n\0\0\0\rIHDR\0\0?\0\0?\b\0\0\0???(\0\0\0\tpHYs\0\0\v\0\0\v\0??\0\0\nOiCCPPhotoshop ICC profile\0\0x?SgTS?

Today its been 3 days since i am stuck at this silly problem , i hope ..

anyone with a solution to this problem or a whole new approach is really appreciable. [Only part i cant change is the PHP code]

SumanP89
  • 119
  • 1
  • 4
  • 13
  • Have your tried `File.ReadAllText(string path)` ? – Philippe Paré Dec 08 '15 at 16:48
  • Is it an encoding problem? Likely why the first method "wasn't working" is that you specified ASCII while the second method will use UTF-8 by default (I think). Maybe you have other encoding mismatches elsewhere? – Broots Waymb Dec 08 '15 at 16:53
  • Sounds like this may help with the UTF isues http://stackoverflow.com/questions/279170/utf-8-all-the-way-through – RiggsFolly Dec 08 '15 at 17:17

2 Answers2

2

Yes there seems to be some problem with encoding set,

This worked for me

StreamReader sr = new StreamReader(filePath);
string sContentsa = sr.ReadToEnd();
sr.Close();
SumanP89
  • 119
  • 1
  • 4
  • 13
-1
        var bodyStream = new StreamReader(Request.InputStream);
        bodyStream.BaseStream.Seek(0, SeekOrigin.Begin);
        var bodyText = bodyStream.ReadToEnd();