C# Web App: Simple file uploading/retrieiving system saving to a SQL database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmj07
    New Member
    • Aug 2008
    • 55

    C# Web App: Simple file uploading/retrieiving system saving to a SQL database

    Can anybody help me with a simple file uploading and retrieving idea?

    What I need is a simple system that uploads the files to be stored in a SQL database which are then shown on the front end in a datagrid, with this datagrid there needs to be a view button where the user can view the file when they return to the page.

    I tried playing with the OpenFileDialog class but to no avail.

    Any help will be much appreciated thanks :)
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    OpenFileDialog won't work on the web. The only way to upload files on the web (at least in ASP.NET) is using the FileUpload control, which isn't very flexible. It just brings up the browser's select file dialog, and you can't tell it what path to start in or anything.

    So you need to use this with a button, and when the user clicks a button, you need to save the file, at least temporarily. Then, you need to use a System.IO.FileS tream to get a byte array of the file. Then, insert that into a database.

    Then you can create your gridview, with one column containing links. And on the page it links to, maybe use a querystring to pass the pk value, then select the file out of the DB and use the Response.WriteF ile method to present it.

    Now, if you really want to have some "fun" with the uploading, you could use a silverlight frontend. But you'd have to write a WCF or ASMX webservice to do the dirty work.

    Hope that helps.

    Comment

    • dmj07
      New Member
      • Aug 2008
      • 55

      #3
      Ah i see what you mean so use the FileBytes of the file upload control to return the bytes content of the uploaded file and then store this in the database I presume as a varbinary(MAX). ..

      Do you have any example of its use with the FileStream as I have never really come across this before?!?

      Many thanks for your help on this :)

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Actually, I was thinking of using the Image column type, but varbinary should work too. The Image type is the same as a BLOB.

        Here's some sample code on getting a byte[] from a file using the FileStream object:
        Code:
        //c#
        FileStream fs = new FileStream(@"c:\dev\ascii.txt", FileMode.Open, FileAccess.Read);
        byte[] buffer = new byte[fs.Length];
        fs.Read(buffer, 0, (int)fs.Length);
        Remember, this is in the System.IO namespace.

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Although, if you are using the FileBytes method, you don't need to use a filestream...yo u already have the byte[] from the file.

          Just use a parameterized insert statement to insert it into your DB.

          Comment

          • dmj07
            New Member
            • Aug 2008
            • 55

            #6
            hey many thanks for your help on this one you've been great :)

            i'll give it a shot in my program and if all goes well will post some sample code

            Comment

            Working...