0

使用 Sql FILESTREAM 进行 Web 应用程序的学习和测试。客户端从网页上传一个大文件,这需要“X”时间,完全上传后显示 100% 完成。但是,非常大的文件也需要时间让 SqlFileStream 写入文件系统,所以我想分拆一个线程来完成该部分。我得到的代码似乎工作正常,但文件流文件中没有数据。

我将初始记录创建包装在它自己的事务范围中,并在线程中使用单独的事务范围。在线程例程中,我有适当的 PathName() 和 TransactionContext 但我假设我在使用线程时遗漏了一些东西。

我已经注释掉了正常工作的正常 SqlFileStream 调用。你能看出我在这里做的有什么问题吗?

    Public Function StoreFileStream()
        Dim json As New Dictionary(Of String, Object)
        Dim parms As New FileStreamThreadParameters

        If HttpContext.Current.Request.Files.Count > 0 Then
            Dim file As HttpPostedFile = HttpContext.Current.Request.Files(0)

            If "contentType" <> String.Empty Then
                Dim fs As Stream = file.InputStream
                Dim br As New BinaryReader(fs)
                Dim noBytes As New Byte()

                Try
                    Dim filePath As String = ""
                    Dim trxContext As Byte() = {}
                    Dim baseFileId As Integer

                    Using trxScope As New TransactionScope
                        Using dbConn As New SqlConnection(DigsConnStr)
                            Using dbCmd As New SqlCommand("ADD_FileStreamFile", dbConn)
                                dbConn.Open()
                                Using dbRdr As SqlDataReader = dbCmd.ExecuteReader(CommandBehavior.SingleRow)
                                    dbRdr.Read()
                                    If dbRdr.HasRows Then
                                        filePath = dbRdr("Path")
                                        trxContext = dbRdr("TrxContext")
                                        baseFileId = dbRdr("BaseFileID")
                                    End If
                                    dbRdr.Close()
                                End Using

                                ' Code below writes to file, but trying to offload this to a separate thread so user is not waiting
                                'Using dest As New SqlFileStream(filePath, trxContext, FileAccess.Write)
                                '    fs.CopyTo(dest, 4096)
                                '    dest.Close()
                                'End Using
                            End Using
                            dbConn.Close()
                        End Using
                        trxScope.Complete()
                    End Using ' transaction commits here, not in line above

                    parms.baseFileId = baseFileId
                    parms.fs = New MemoryStream
                    fs.CopyTo(parms.fs)

                    Dim fileUpdateThread As New Threading.Thread(Sub()
                                                                     UpdateFileStreamThreaded(parms)
                                                                 End Sub)
                    fileUpdateThread.Start()

                    json.Add("status", "success")
                Catch ex As Exception
                    Elmah.ErrorSignal.FromCurrentContext().Raise(ex)
                    json.Add("status", "failure")
                    json.Add("msg", ex.Message)
                    json.Add("procedure", System.Reflection.MethodBase.GetCurrentMethod.Name)
                End Try
            Else
                json.Add("status", "failure")
                json.Add("msg", "Invalid file type")
                json.Add("procedure", System.Reflection.MethodBase.GetCurrentMethod.Name)
            End If
        End If
        Return json
    End Function

    Public Class FileStreamThreadParameters
        Public Property baseFileId As Integer
        Public fs As Stream
    End Class

    Private Sub UpdateFileStreamThreaded(parms As FileStreamThreadParameters)
        Dim filePath As String = ""
        Dim trxContext As Byte() = {}

        Try
            Using trxScope As New TransactionScope
                Using dbConn As New SqlConnection(DigsConnStr)
                    Using dbCmd As New SqlCommand("SELECT FileBytes.PathName() 'Path', GET_FILESTREAM_TRANSACTION_CONTEXT() 'TrxContext' FROM FileStreamFile WHERE Id = " & parms.baseFileId, dbConn)
                        dbConn.Open()
                        Using dbRdr As SqlDataReader = dbCmd.ExecuteReader(CommandBehavior.SingleRow)
                            dbRdr.Read()
                            If dbRdr.HasRows Then
                                filePath = dbRdr("Path")
                                trxContext = dbRdr("TrxContext")
                            End If
                            dbRdr.Close()

                            Using dest As New SqlFileStream(filePath, trxContext, FileAccess.Write)
                                parms.fs.CopyTo(dest, 4096)
                                dest.Close()
                            End Using
                        End Using
                    End Using
                    dbConn.Close()
                End Using
                trxScope.Complete()
            End Using
        Catch ex As Exception
            'Elmah.ErrorSignal.FromCurrentContext().Raise(ex)
        End Try
    End Sub
4

1 回答 1

0

显然,这是一个复杂的问题。我实际上让它与下面的代码一起工作。但是,由于设置起来过于复杂,我最终完全放弃了使用 SQL FILESTREAM。

这是一个现有的 Web 应用程序,其中 sql 服务器位于不同的盒子上。在我让文件流工作后,下一个障碍是身份验证设置。Filestream 需要您的连接字符串上的集成安全性。即使在我们的 Intranet 应用程序上使用 Windows 身份验证,我也无法让 Web 应用程序在连接到数据库时使用 Windows 凭据。它似乎总是使用计算机名称或应用程序池服务。我尝试了很多我在网上找到的例子,但都无济于事。即使我让它工作,我也想使用 Active Directory 组而不是个人登录,这看起来是另一个障碍。

此应用程序将文档存储在 varbinary 列中,以便可以在某个时候启用全文搜索。问题在于通常是图片或视频的大文件。由于无论如何您都无法在这些文本上搜索文本,因此现在的策略是将它们存储在文件系统中,并且所有其他可搜索的文档(.docx、.pptx 等)仍将存储在 varbinary 中。我真的很难过我无法让文件流工作,因为它似乎是理想的解决方案。有一天我会回到它,但它真的不应该那么复杂。:-(

我得到的代码是:

                    Dim filePath As String = ""
                    Dim trxContext As Byte() = {}
                    Dim baseFileId As Integer

                    Using trxScope As New TransactionScope
                        Using dbConn As New SqlConnection(DigsFSConnStr)
                            Using dbCmd As New SqlCommand("NEW_FileStreamBaseFile", dbConn)
                                dbCmd.CommandType = CommandType.StoredProcedure
                                dbCmd.Parameters.AddWithValue("@Title", fileDesc)
                                dbCmd.Parameters.AddWithValue("@Summary", summary)
                                dbCmd.Parameters.AddWithValue("@Comments", comments)
                                dbCmd.Parameters.AddWithValue("@FileName", uploadedFileName)
                                dbCmd.Parameters.AddWithValue("@ContentType", contentType)
                                dbCmd.Parameters.AddWithValue("@FileExt", ext)
                                'dbCmd.Parameters.AddWithValue("@FileBytes", noBytes)    ' now that were offloading the file byte storage to a thread
                                dbCmd.Parameters.AddWithValue("@UploadedByResourceID", uploadedByResourceID)
                                dbCmd.Parameters.AddWithValue("@UploadedByShortName", uploadedByShortName)
                                dbCmd.Parameters.AddWithValue("@FileAuthor", fileAuthor)
                                dbCmd.Parameters.AddWithValue("@TagRecID", tagRecID)
                                dbCmd.Parameters.AddWithValue("@UserID", samAccountName)
                                dbCmd.Parameters.AddWithValue("@FileDate", fileDate)
                                dbCmd.Parameters.AddWithValue("@FileType", fileType)
                                dbCmd.Parameters.AddWithValue("@FileTypeRecID", fileTypeRecId)

                                ' Save to file system too for xod conversion
                                file.SaveAs(HttpContext.Current.Server.MapPath("~/files/uploaded/") & uploadedFileName)

                                dbConn.Open()
                                Using dbRdr As SqlDataReader = dbCmd.ExecuteReader(CommandBehavior.SingleRow)
                                    dbRdr.Read()
                                    If dbRdr.HasRows Then
                                        filePath = dbRdr("Path")
                                        trxContext = dbRdr("TrxContext")
                                        json.Add("baseFileId", dbRdr("BaseFileID"))
                                        virtualFileRecId = dbRdr("VirtualFileRecID")
                                        dbStatus = dbRdr("status")
                                        If dbStatus = "failure" Then
                                            json.Add("msg", dbRdr("msg"))
                                        End If
                                    End If
                                    dbRdr.Close()
                                End Using

                                ' Prepare and start Task thread to write the file
                                If dbStatus = "success" Then
                                    bytes = br.ReadBytes(fs.Length)
                                    Dim task As New System.Threading.Tasks.Task(
                                        Sub()
                                            UpdateNewFileStreamBytes(virtualFileRecId, bytes)
                                        End Sub)
                                    task.Start()
                                    json.Add("status", "success")
                                Else
                                    json.Add("status", "failure")
                                End If
                            End Using
                            dbConn.Close()
                        End Using
                        trxScope.Complete()
                    End Using ' transaction commits here, not in line above

任务流程为:

    Private Sub UpdateNewFileStreamBytes(virtualFileRecId As Integer, fileBytes As Byte())
        Dim filePath As String = ""
        Dim trxContext As Byte() = {}

        Try
            Using trxScope As New TransactionScope
                Using dbConn As New SqlConnection(DigsFSConnStr)
                    Using dbCmd As New SqlCommand("UPD_FileStreamBaseFile", dbConn)
                        dbCmd.CommandType = CommandType.StoredProcedure
                        dbCmd.Parameters.AddWithValue("@VirtualFileRecID", virtualFileRecId)

                        dbConn.Open()
                        Using dbRdr As SqlDataReader = dbCmd.ExecuteReader(CommandBehavior.SingleRow)
                            dbRdr.Read()
                            If dbRdr.HasRows Then
                                filePath = dbRdr("Path")
                                trxContext = dbRdr("TrxContext")
                            End If
                            dbRdr.Close()

                            Using dest As New SqlFileStream(filePath, trxContext, FileAccess.Write)
                                dest.Write(fileBytes, 0, fileBytes.Length)
                                dest.Close()
                            End Using
                        End Using
                    End Using
                    dbConn.Close()
                End Using
                trxScope.Complete()
            End Using
        Catch ex As Exception
            Elmah.ErrorSignal.FromCurrentContext().Raise(ex)
        End Try
于 2016-02-19T17:11:32.523 回答