共用方式為


Web App Design: Avoid Writing to Disk

I get this question about once per month:

I have a web app that accepts an image or XML document. I need to save it to the database. I first save it to file, then how do I get the file uploaded to the database?

The short answer is: you don't.  If you don't need to hit the disk, then just say no.  Instead, get the bytes from the request stream that you are interested in (the bytes representing the image or XML document) and stick them directly into the database.

 if (this.FileUpload1.HasFile)
{
    if ((this.FileUpload1.PostedFile.ContentType == "image/pjpeg") 
&& (this.FileUpload1.PostedFile.ContentLength < 100000 ))
    {            
        SqlConnection cn = new SqlConnection("Database=AdventureWorks;
Server=(local);Integrated Security=true;");
        cn.Open();
        SqlCommand insert = new SqlCommand(
"INSERT INTO Production.ProductPhoto(LargePhoto, ModifiedDate) VALUES (@photo,getdate())",
cn);
        SqlParameter param = insert.CreateParameter();
        param.ParameterName = "@photo";
        param.SqlDbType = SqlDbType.VarBinary; 
        param.Direction = ParameterDirection.Input;            
        param.Value = FileUpload1.FileBytes; 
        insert.Parameters.Add(param);
        try
        {
            int rows = insert.ExecuteNonQuery();
        }
        catch (SqlException oops)
        {
            Response.Write(oops.ToString());
        }
        finally
        {
            insert.Dispose();
            cn.Close();
            cn.Dispose();
        }
    }
}

This example is using the FileUpload control for ASP.NET, but the effect is the same for a posted XML document: you just acquire the byte array for the stream and stick that directly into the database, no need to hit the disk.

Comments

  • Anonymous
    March 10, 2006
    And then to turn it back into an image to display?

    Don't you hit the reverse problem with needing to save the image to a file for displaying in a webpage?
    Isn't the overhead for rendering the image (which will happen far more frequently than storage) going to be higher than a one time hit for persisting the image to a file?
  • Anonymous
    March 11, 2006
    Dasher left an interesting comment to my previous post on avoiding writing to disk:

    Don't you hit...
  • Anonymous
    March 11, 2006
    You know your specific application requirements, I can only make assumptions about what you are trying to achieve.  In the question I posted, there is a requirement to store the data in the database.  The file is simply an interim step to get the data into the database.

    What if the app is using forms authentication in a web farm and you want to secure the image from other application users?  You can store the image to disk, but then you run into the problem of replicating the image to each and every server on the farm... something that can be very difficult to achieve in a reliable fashion, especially under heavy load.  Storing the image in the database becomes a very attractive option here.

    As for rendering the image: no, you do not have to write the image to file again to render it.  I wrote up a follow-up posting to show how to do this:

    http://blogs.msdn.com/kaevans/archive/2006/03/11/549419.aspx