【问题标题】:storing pdf files using filestream on file system使用文件系统上的文件流存储 pdf 文件
【发布时间】:2011-12-16 05:21:12
【问题描述】:

我第一次尝试使用 filestream 在文件系统上使用 varbinary(MAX) 列类型的 DB 存储 pdf 文件。

我已按照以下步骤操作。

  1. 在 SQL Server 2008 R2 上启用了文件流功能。
  2. 为 BLOB 存储创建文件组
  3. 创建的表包含 varbinary(max) 类型的 blob 列

现在,我想使用文件上传控件来选择文件,当点击上传按钮时,它应该保存 pdf 文件。另外,如何找回文件?


我试过下面的代码

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();
            }
        }

    }

上面的代码显示错误如下

操作系统在“D:\DB\FS\d11132f8-c2a8-452d-ae0c-208164a550d7”上尝试“NtCreateFile”时返回错误“0xc000003a({Path Not Found}路径 %hs 不存在。)” \beb8e1f1-8116-440b-870b-7cef4281a15d\0000001c-000000e4-010d'。该语句已终止。

那么,这有什么线索吗?

【问题讨论】:

  • 谁能帮我解决这个问题?

标签: pdf filestream


【解决方案1】:

如果您使用 SSMS 表设计器更改了表,则 FILESTEAM 列属性将丢失,从而导致找不到路径。通过在数据库中运行以下语句,确保为文件字段设置了 FILESTREAM 属性:

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

您需要通过脚本而不是 SSMS 更改表,以将您的 varchar(max)/filestream 字段绑定到您应该已经创建的 FileGroup。

当我遇到这个问题时,我在 StackOverflow 上找到了答案,但似乎无法再次找到它以供参考。

【讨论】:

    【解决方案2】:

    我知道这是旧的,但供将来参考:

    我们检查了@BMP 建议的 SERVERPROPERTY 值。它们配置正确,所以没有帮助。

    但是,我们继续关闭了文件流访问的 Windows 文件共享部分。完成此操作后,错误就消失了。

    在我们的例子中,它是一个 Web 应用程序,它运行在与出现问题的 sql 服务器完全相同的机器上。我不确定网络应用程序的应用程序池用户是否无权访问 Windows 创建的文件共享。

    详情如下:

    Windows 2003 服务器 (x86)
    IIS 6
    SQL Server 2008 R2 速成版


    更新:显然这工作了几天。它不再工作了。

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 1970-01-01
      • 2012-07-02
      • 2012-05-08
      • 2017-09-22
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      相关资源
      最近更新 更多