我有一个内部网络应用程序,它将文件存储在 SQL filestream 中。
在我的开发机器上,一切都很顺利。
我能够上传和存储文件到 SQL filestream (AjaxUpload) 并能够下载它们。
在生产服务器上,我能够上传文件、删除它们,但是当我尝试从 filestream 下载文件时,我会收到以下错误:
Access is denied
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ComponentModel.Win32Exception: Access is denied
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[Win32Exception (0x80004005): Access is denied]
System.Data.SqlTypes.SqlFileStream.OpenSqlFileStream(String path, Byte[] transactionContext, FileAccess access, FileOptions options, Int64 allocationSize) +1465594
System.Data.SqlTypes.SqlFileStream..ctor(String path, Byte[] transactionContext, FileAccess access, FileOptions options, Int64 allocationSize) +398
System.Data.SqlTypes.SqlFileStream..ctor(String path, Byte[] transactionContext, FileAccess access) +27
quotes_GetFileStream.quotes_GetFileStream_Load(Object sender, EventArgs e) +740
System.Web.UI.Control.LoadRecursive() +71
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3048
应用程序池设置为集成模式,版本为V.4.0,并且我使用域用户进行身份验证返回到SQL。
我为该应用程序的SQL数据库授予了该用户DB_Owner权限。
甚至尝试将所有SQL Server角色都赋予它,但仍然出现上述错误。
当我将Identity用户名更改为我的用户名(我拥有域管理员权限)时,在实际服务器上一切都运行得很顺利。
我缺少哪些权限以便让该用户能够正确地从SQL文件流中读取。
这是获取文件并将其推送到浏览器的代码块,也许我在这里漏掉了什么(尽管一旦修改了用户,它就可以正常工作)。
Dim RecId As Integer = -1
If Not IsNothing(Request.QueryString("ID")) And IsNumeric(Request.QueryString("ID")) Then
RecId = CType(Request.QueryString("ID"), Integer)
End If
Dim ConString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ToString
Using Con As SqlConnection = New SqlConnection(ConString)
Con.Open()
Dim txn As SqlTransaction = Con.BeginTransaction()
Dim Sql As String = "SELECT FileData.PathName() , GET_FILESTREAM_TRANSACTION_CONTEXT() as TransactionContext, [FileName], [FileExtension] FROM [QAttach] where RecId = @RecId"
Dim cmd As SqlCommand = New SqlCommand(Sql, Con, txn)
cmd.Parameters.Add("@RecID", Data.SqlDbType.Int).Value = RecId
Dim Rdr As SqlDataReader = cmd.ExecuteReader()
While Rdr.Read()
Dim FilePath As String = Rdr(0).ToString()
Dim objContext As Byte() = DirectCast(Rdr(1), Byte())
Dim fname As String = Rdr(2).ToString()
Dim FileExtension As String = Rdr(3).ToString()
Dim sfs As SqlFileStream = New SqlFileStream(FilePath, objContext, FileAccess.Read)
Dim Buffer As Byte() = New Byte(CInt(sfs.Length) - 1) {}
sfs.Read(Buffer, 0, Convert.ToInt32(Buffer.Length))
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = FileExtension
Response.AddHeader("content-disposition", "attachment;filename=" & fname)
Response.BinaryWrite(Buffer)
Response.Flush()
Response.End()
sfs.Close()
End While
End Using
Thanks
Oren