storing pdf files using filestream on file system

1

I am first time trying to use filestream to store pdf files on file system using varbinary(MAX) column type of DB.

I have followed following steps.

  1. enabled filestream feature on SQL server 2008 R2.
  2. Create a filegroup for BLOB storage
  3. created table with blob column of type varbinary(max)

Now, I want to use file upload control to select file and when click on upload button it should save the pdf file. Also, how to retrieve the file?


I have tried following code

protected void btnFSupload_Click(object sender, EventArgs e)
    {
        SqlConnection cn = null;
        SqlTransaction tx = null;
        SqlCommand cmd = null;
        SqlCommand cmd2 = null;
        bool bCommit = false;

        try
        {
            // read in the file to be saved as a blob in the database
            FileStream input = new FileStream(@"D:\swami.pdf", FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[(int)input.Length];

            input.Read(buffer, 0, buffer.Length);

            cn = new SqlConnection("server=at-hetal01\\sqlexpress;Initial Catalog=practice;Integrated Security=true;");
            cn.Open();

            tx = cn.BeginTransaction();

            cmd = new SqlCommand("dbo.stp_AddBLOB", cn, tx);

            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            SqlDataReader r = cmd.ExecuteReader(System.Data.CommandBehavior.SingleRow);

            r.Read();

            string id = r[0].ToString();
            string path = r[1].ToString();
            r.Close();  

            // get the transaction context
            cmd2 = new SqlCommand("SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()", cn, tx);
            Object obj = cmd2.ExecuteScalar();
            byte[] txCtx = (byte[])obj;

            // open the filestream to the blob
            SafeFileHandle handle = OpenSqlFilestream(path,DESIRED_ACCESS_WRITE,SQL_FILESTREAM_OPEN_NO_FLAGS,txCtx,(UInt32)txCtx.Length,0);

            // open a Filestream to write the blob
            FileStream output = new FileStream(handle,FileAccess.Write,buffer.Length,false);
            output.Write(buffer,0,buffer.Length);
            output.Close();

            if (handle != null && !handle.IsClosed)
                handle.Close();

            bCommit = true;
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            if (cn != null)
            {
                switch (bCommit)
                {
                    case true:
                        tx.Commit();
                        break;

                    case false:
                        tx.Rollback();
                        break;
                }

                cn.Close();
            }
        }

    }

Above code shows error as below

The operating system returned the error '0xc000003a({Path Not Found} The path %hs does not exist.)' while attempting 'NtCreateFile' on 'D:\DB\FS\d11132f8-c2a8-452d-ae0c-208164a550d7\beb8e1f1-8116-440b-870b-7cef4281a15d\0000001c-000000e4-010d'. The statement has been terminated.

So, any clue on this?

pdf
filestream
asked on Stack Overflow Dec 16, 2011 by ghetal • edited Dec 17, 2011 by Bill the Lizard

2 Answers

0

If you have altered your table using SSMS table designer, the FILESTEAM column attribute will be lost producing the path not found. Make sure the FILESTREAM attribute is set for the file field by running the follwoing statement in your database:

select SERVERPROPERTY('FilestreamShareName') as ShareName,
       SERVERPROPERTY('FilestreamConfiguredLevel') as ConfiguredLevel,
       SERVERPROPERTY('FilestreamEffectiveLevel') as EffectiveLevel

You'll need to alter the table via a script and NOT SSMS to tie your varchar(max)/filestream field to the FileGroup you should have already created.

When I ran into this issue, I found the answer on StackOverflow, but can't seem to find it again for the reference.

answered on Stack Overflow Jan 14, 2012 by BMP
0

I know this is old, but for future reference:

We checked the SERVERPROPERTY values that @BMP suggested. They were configured correctly, so that didn't help.

However, we went ahead and turned OFF the windows file share part of the file streaming access. Once this was done, the error went away.

In our case it was a web app running on the exact same machine as the sql server which exhibited the problem. I'm not sure if the web app's app pool user didn't have access to the file share created by windows or not.

The details were:

Windows 2003 Server (x86)
IIS 6
SQL Server 2008 R2 Express


UPDATE: Apparently this worked for a few days. It's not working any more.

answered on Stack Overflow Sep 5, 2012 by NotMe • edited Sep 13, 2012 by NotMe

User contributions licensed under CC BY-SA 3.0