【问题标题】:ASP.NET C# File AccessASP.NET C# 文件访问
【发布时间】:2011-06-06 06:20:41
【问题描述】:

我的“ImageDemo”应用程序的架构

技术:ASP.NET 语言:C#

以前的架构: asp.net 页面存储在网络服务器中。 网络服务器中存在一个名为 ImageRepository 的文件夹。我确实将图像(由用户浏览、选择和存储)存储在 ImageRepository 中。我可以访问存储在 ImageRepository 中的图像的所有属性和内容。我曾经使用 ASP.NET 的 Image 控件来显示图像。

新架构: 我没有将图像存储在网络服务器的文件夹中,而是使用以下凭据(比如)设置了一个专用的 文件服务器

用户名:我的名字

密码:wXy12Apl

可以登录系统,访问我存储图像的文件夹,读取文件的所有属性。然后我将文件名添加到下拉列表中,所以它看起来像这样

所以在 onselectedindexchanged 事件中,我尝试使用 ASP.NET 的 Image 控件来显示图像。但我不能。会有什么问题?我可以访问文件的所有属性,但无法读取图像的内容

例如;当我选择第一张图片时,控制看起来像

当我选择第二张图片时,控制看起来像

【问题讨论】:

  • 请贴一些相关代码。

标签: c# asp.net permissions filesystems


【解决方案1】:

您可能没有该文件的权限。您可以像@Smudge202 建议的那样做并应用正确的权限,或者您可以在不同的上下文中运行部分代码。请参阅此代码以模拟用户:http://www.codeproject.com/KB/cs/zetaimpersonator.aspx

【讨论】:

    【解决方案2】:

    浏览器/最终用户无权访问文件服务器。您可以实施几个快速的解决方案:

    1) 为最终用户添加从新文件共享读取的权限 - 如果您的站点是内部站点(仅在 LAN 上),那么这可能是最佳解决方案。如果网站暴露在互联网上,这可能不是最安全的解决方案。

    2) 创建一个简单的 Web 服务(WCF 或 ASMX)。确保服务可以从新共享中读取,并让服务将内容作为网络响应返回。

    这是后者的一个(几乎完整的)示例,一段旧代码响应实现 IHttpHandler 的旧式 asp.net 处理程序 (ashx) - 我建议个人使用 WCF:

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        _Context = context
        _Context.Response.Clear()
        _Context.Response.ContentType = "image/jpg"
        If ImageParameter IsNot Nothing Then
    
            Dim imageData As Byte() = Nothing
            If String.IsNullOrEmpty(ImageParameter.CompressedFileStorageLocation) Then
                Return
            End If
    
            Dim imagePath As String = String.Format("{0}{1}", CS_IMAGE_REPOSITORY_BASE_PATH, ImageParameter.CompressedFileStorageLocation)
            Try
                If IO.File.Exists(imagePath) Then
                    Using s As FileStream = New FileStream(imagePath, FileMode.Open)
                        ReDim imageData(s.Length)
                        Dim bytesRead As Integer = s.Read(imageData, 0, s.Length)
                        s.Close()
                    End Using
                End If
            Catch ex As Exception
                Debug.Print(ex.Message)
            End Try
    
            _Context.Response.OutputStream.Write(imageData, 0, imageData.Length)
        End If
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多