Microsoft asp.net question (long shot!!)

mossmotorsport
mossmotorsport Posts: 356
edited January 2011 in The Crudcatcher
Hi guys,

I know this is a long shot, but I know some of you are fellow computer geeks therefore hopefully someone can give me a hand (yeehaamcgee :wink: )

I've spent ages trying to find a decent tutorial to allow users to upload an image to an SQL Server Database. This is all for my dissertation, so the scenario would be that users have a form to enter the trail name, trail location etc, but then have the option to upload an image or thumbnail. YouTube tutorials don't seem that great, so any ideas?
Specialized FSR XC Expert 2010

Welcome!

It looks like you're new here. Sign in or register to get started.

Comments

  • I wish it was just a case of following a tutorial on there, honestly :cry:
    Specialized FSR XC Expert 2010
  • Gazlar
    Gazlar Posts: 8,083
    have you tried turning it off and back on again?
    Mountain biking is like sex.......more fun when someone else is getting hurt
    Amy
    Farnsworth
    Zapp
  • Simonb256
    Simonb256 Posts: 880
    Ok,

    My first question is why are you trying to store the image in the DB? Personally I would store the path to the image/image name in the db and upload the image to a specific directory on the webspace.

    To store an actual image in to sql server, you need to read image file into a byte array. Something akin to this: http://www.codeproject.com/KB/database/ ... Serve.aspx However I have not read through it properly as Im about to go home.

    Though the first option is possibly the easiest and safest option.
    "War is Peace; Freedom is Slavery; Ignorance is Strength." George Orwell - 1984
  • VWsurfbum
    VWsurfbum Posts: 7,881
    Simonb256 wrote:
    Though the first option is possibly the easiest and safest option.
    I understood this bit, nothing else.
    Kazza the Tranny
    Now for sale Fatty
  • Which bit are you struggling with, storing the image in the database or uploading the file from the client?
    Use the Fileupload control for the second bit, but as Simon says, leave the image file outside the database.
    Northwind wrote: It's like I covered it in superglue and rode it through ebay.
  • I did consider the disadvantages of storing an image into an SQL Database - as obviously the more rows get populated with images, the more bloated and slower the DB will become.

    Truth be told, I've been thrown in the deep end doing this for my dissertation and I don't properly understand the best way of doing what I need to achieve. Your recommendation then would be to forget storing the image into a DB and just upload straight to a directory and then reference that directory?
    Specialized FSR XC Expert 2010
  • Yes.
    Store the path to where you've stored the image.
    Northwind wrote: It's like I covered it in superglue and rode it through ebay.
  • bentes
    bentes Posts: 286
    Yes.
    Store the path to where you've stored the image.
    I agree.

    I would store the path in the database and the image in some folder in the website.

    Remember that the aspnet user has to have write permissions to it.
  • does anyone have any experience then such a web application, or does anyone have recommendations for an open source asp.net photo gallery that they have used?
    Specialized FSR XC Expert 2010
  • Tartanyak
    Tartanyak Posts: 1,538
    There's a file upload control - I assume you're using asp.net 3.5? Or at least 2... Anyhoo, tutorial video:
    http://www.asp.net/general/videos/how-d ... -in-aspnet
    That's got a useful lump of code in the last post:
    http://forums.asp.net/p/1422054/3157640.aspx


    That'll get you the data to the server. Nice and easy.

    If you're talking thumbnails, are you restricting the file size or the image size itself? Or, scaling when you've got the file.

    <ponders> To convert the uploaded file to an image without saving first, it'd be something simple like (code stolen from http://msdn.microsoft.com/en-us/library ... dfile.aspx if you wanna look):
    Ooh, disclaimer - may have slight errors in, but it'll give you the gist. Don't have Visual Studio on this machine and don't have the time to test.
            System.IO.Stream myStream;
            Int32 fileLen;
            System.Drawing.Image imageToPlayWith;
            System.Drawing.Image.thumbnail;
            String filepath = 'somewhereonserver\thumbnails\'
    
            // Get the length of the file.
            fileLen = FileUpload1.PostedFile.ContentLength;
            // Create a byte array to hold the contents of the file.
            Byte[] Input = new Byte[fileLen];
            // Initialize the stream to read the uploaded file.
            myStream = FileUpload1.FileContent;
            // Read the file into the byte array.
            myStream.Read(Input, 0, fileLen);
            // Create the image from the stream
            imageToPlayWith = Image.FromStream(myStream);
            // Whiittle out a thumbnail. Needs a little setup - change the '100' bits to the size you want
            Image.GetThumbnailImageAbort callback = 
                    new Image.GetThumbnailImageAbort(ThumbnailCallback);
            thumbnail = imageToPlayWith.GetThumbnailImage(100, 100, callback, new IntPtr());
            // So you have thumbnail as an Image Class. do with it as you will. 
            // I'd do something like produce a GUID for the filename, then do as Simon said and
            // save it to the machine somewhere with a file reference in the DB.
           System.Guid thumbnailUniqueFilename = Guid.NewGuid();
           String thumbnailFullFilepath = filepath + thumbnailUniqueFilename.ToString() + .jpg
           //Save the thumbnail
           thumbnail.Save(thumbnailFullFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
           // Do whatever you'd do to save the filename to the DB. You can figure that bit out.
           
           //Kill the stream from the file upload, since we don't need it any more.
           myStream.Close();
           
            
    

    That's crap code - but it'll give you the idea. Maybe.
  • cee
    cee Posts: 4,553
    I did consider the disadvantages of storing an image into an SQL Database - as obviously the more rows get populated with images, the more bloated and slower the DB will become.

    Not if you store it as a BLOB (Binary Large Object). Although this can be a bit of a ball ache, particularly around reading the binary out of the DB and re-creating the image on load.

    The SQL Server datatype Image is a bit like this, but stores a byte array. Then when you write it into a file (a virtual file in memory on the asp.net server), it becomes the actual picture.

    like this....http://www.codeproject.com/KB/database/ ... aBase.aspx

    However, performance wise....you will struggle in a uni project to even dent the perfromance of SQL Server, unless the design of the structure and queries is terrible. We have client SQL Server DB's with upwards of 50 gigs of data, in tables with millions of rows. Some rows are well over 100k each, so depending on how you design the table will have a bigger impact on performance than simply the number of rows.
    Truth be told, I've been thrown in the deep end doing this for my dissertation and I don't properly understand the best way of doing what I need to achieve. Your recommendation then would be to forget storing the image into a DB and just upload straight to a directory and then reference that directory?

    This is the most common way of storing images for website....simply upload to a directory, then as you say, store the path to the image in the DB. This also has disadvantages in that updating the image means deleting the old one, then uploading the new on. DB updates are obviously far more simple and can be done in one move. Also, the application pool user will require rights to read and write to that directory.
    Whenever I see an adult on a bicycle, I believe in the future of the human race.

    H.G. Wells.
  • Tartanyak wrote:
    There's a file upload control - I assume you're using asp.net 3.5? Or at least 2... Anyhoo, tutorial video:
    http://www.asp.net/general/videos/how-d ... -in-aspnet
    That's got a useful lump of code in the last post:
    http://forums.asp.net/p/1422054/3157640.aspx

    Thanks for that links - it's given me some of the info I was after. Now I'm thinking I need a proper photo gallery, with categories, so I'm looking at Gallery Server Pro which is Open Source.

    All of these links have given me a hand, so cheers. I then need to work out how to do the same thing with Videos! :shock:
    Specialized FSR XC Expert 2010
  • tofu21
    tofu21 Posts: 359
    Keeping images in the DB as it's benefits. Off the top of my head:
    Means they get backed up with the data
    Easier to get consistency if things crash
    If you use a cluster, it's easier than having a shared file system
    Can use DB encryption
    Can use DB auditing and access controls
    Easier to migrate between platforms

    Saying that:
    it will have a higher storage overhead
    And increase code complexity
    DB CPUs are expensive to licence so off loading image storage to a file server makes sence

    Hit google there will be lts of stuff out there. Its worth justifying why you have chosen your approach you'll get points for it.

Welcome!

It looks like you're new here. Sign in or register to get started.