1

Hi I'm not asking here for a totally-mad ecode, more a method, an idea, a "how to" :

I have some files in my SQL server, pdf, txt... And I want to display them :

Here's the code to take the files from the SQL server :

    public Byte[] readFichier(string idFichier)
    {
        connexionString = getConnexionString();

        // la commande à executer sur SQL
        // on recupere le nom de la table dans web.config
        string table = WebConfigurationManager.AppSettings["tableFichiers"];
        string requestString = "SELECT * from " + table + " where FichierID=@FichierID";

        // ouverture de la connection
        SqlConnection connection = new SqlConnection(connexionString);
        SqlCommand command = new SqlCommand(requestString, connection);

        // on ajoute la valeur au parametre
        command.Parameters.AddWithValue("@FichierID", idFichier);

        DataTable tableDeDonnee = new DataTable();
        SqlDataAdapter donneesSQL = new SqlDataAdapter();

        donneesSQL.SelectCommand = command;
        donneesSQL.Fill(tableDeDonnee);

        // recuperation du fichier
        Byte[] bytes = (Byte[])tableDeDonnee.Rows[0]["Fichier"];
        return bytes;
    }

Then I transform it into and html label

[WebMethod]
public static string lireFichierStatique(string id)
{
    var page = (WebForm1)HttpContext.Current.CurrentHandler;
    Byte[] fichier=page.readFichier(id);
    string stringFichier = Convert.ToBase64String(fichier, 0, fichier.Length);

    System.Web.UI.WebControls.Label label = new System.Web.UI.WebControls.Label();
    label.Text = stringFichier;

    // transform the string into html code
    var stringWriter = new StringWriter();
    using (var htmlWriter = new HtmlTextWriter(stringWriter))
    {
          label.RenderControl(htmlWriter);
    }
    string labelString = stringWriter.ToString();

    // return on line for more libibility
    for (int i = 40; i < labelString.Length; i+=30 )
    {
         labelString=labelString.Insert(i, "<br>");
    }
    return labelString;
}

With this code, I succeed to take my file as Bytes, or convert this Bytes as string : But I want it to be more.. readable.

It gives me something like "Q29kZSBkZSBiYXNlIDoNCg0KZm 9yIC0+IEphdmENCndoaWxlIA0K DQoNCmZvcihpbnQgaT0wOyBpPD ..." even for a .txt .

The only piece of code that I need is :

How can I transform an SQL file into a readable string ? Or better, how can I make a window with this file perfectly represented (pdf, image...) but I think that will be out of my skills. And if I don't save a temp file, it's better, Then : How can I adapt that to every type of file ? I need at least .pdf and .txt, but if it works for a .txt, it will be already great !

Thanks !

EDIT : From cubrr

        var page = (WebForm1)HttpContext.Current.CurrentHandler;
        Byte[] fichier=page.readFichier(id);

        string stringFichier = Encoding.UTF8.GetString(fichier);

        System.Web.UI.WebControls.Label label = new System.Web.UI.WebControls.Label();
        label.Text = stringFichier;

        // on transforme ce tableau en string HTML
        var stringWriter = new StringWriter();
        using (var htmlWriter = new HtmlTextWriter(stringWriter))
        {
            label.RenderControl(htmlWriter);
        }
        string labelString = stringWriter.ToString();



        return labelString;

New Edit : How can I detect the encoding/codepage of a text file works fine for .txt, I'll see for others files (I think that I'll need another thing, antother lib for that, any advice is good but the core question is answered)

Last edit :

  public string pdfByteToString(Byte[] fichier)
    {
        string fichierString = "";
        PdfReader reader = new PdfReader(fichier);

        for (int i = 1; i <= reader.NumberOfPages; i++) 
        {
            fichierString+=iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, i);
        }

        return fichierString;
    }

iTextSharp works fine for pdf, i'll find something for .doc, etc

Community
  • 1
  • 1
Pingu
  • 53
  • 10
  • if you don't want to write inbrowser viewer for all this types, you can just propose user to download file. – Kirill Bestemyanov Jul 13 '15 at 07:58
  • I alreadey succeed to make the user download the file, or upload it, but the idea is here, only display an extract of the file before download it – Pingu Jul 13 '15 at 08:12
  • 2
    If the file contains text contents, use whatever encoding the file's encoded in (if you're not sure, UTF8 is a good starting point) to get the string from the bytes. `Encoding.UTF8.GetString(byte[])`. If it's a PDF, you'll need to use something else. – cbr Jul 15 '15 at 07:57
  • Thanks ! It seems to work for .txt, with somes � but it's readable : I'll see for others files – Pingu Jul 15 '15 at 08:19

0 Answers0