0

i need to pick up an excel file form my asp.net mvc project folder and encode it in base64 string.

mine code right now:

 string base64 = String.Empty;
 var pathName = Server.MapPath("~/App_Data/ImportTemplate.xlsx");`
 byte[] docBytes = null;

using (StreamReader strm = new StreamReader(pathName, System.Text.Encoding.UTF8))
        {
            Stream s = strm.BaseStream;
            BinaryReader r = new BinaryReader(s);
            docBytes = r.ReadBytes(Convert.ToInt32(r.BaseStream.Length));
            base64 = Convert.ToBase64String(docBytes);
            r.Close();
            s.Close();
            strm.Close();
        }

so far this doesn't work properly. Any suggestions?

mat1c
  • 95
  • 3
  • 12

2 Answers2

0

Most likely the problem is that your base64 data contains '+' and '/' characters, which are interpreted specially by HTTP. You need to convert that binary to what they call base64 URL encoding, which uses '-' and '_' for those characters. The examples at http://api.adform.com/Services/Documentation/Samples/MediaPlanServiceSamples.htm#ImportMediaPlan indicate that this is the case.

See https://stackoverflow.com/a/17032614/56778 for information on how to do the conversion.

Community
  • 1
  • 1
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
0

Can u try:

 byte[] docBytes = ReadFile(pathName);


 byte[] ReadFile(string sourcePath)
    {
        byte[] data = null;
        FileInfo fileInfo = new FileInfo(sourcePath);
        long numBytes = fileInfo .Length;
        FileStream fileStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fileStream);
        data = br.ReadBytes((int)numBytes);
        fileStream .Close();
        return data;
    }
CocLn
  • 742
  • 10
  • 15