Asp.net mvc ASP.net MVC项目中存储在文件系统上的照片出现零星访问被拒绝错误 一些背景

Asp.net mvc ASP.net MVC项目中存储在文件系统上的照片出现零星访问被拒绝错误 一些背景,asp.net-mvc,filesystems,photo,access-denied,Asp.net Mvc,Filesystems,Photo,Access Denied,我有一个ASP.NETMVC4Web应用程序,它提供了对大量照片的访问。每个照片都从MySQL表中获得一个唯一的ID,然后以十六进制形式组成文件名并存储在文件系统的文件夹中 例如: D:\Photos\69F.jpg 在整个应用程序中,将显示使用原始照片创建的不同大小的缩略图。由于缩略图很少发生更改,因此,如果有更改,缩略图将以以下格式创建并保存到同一文件系统文件夹中。(这意味着90%的时间,照片可以直接返回,无需任何图像处理) 缩略图正常-D:\Photos\69F\u t.jpg 缩略图-

我有一个ASP.NETMVC4Web应用程序,它提供了对大量照片的访问。每个照片都从MySQL表中获得一个唯一的ID,然后以十六进制形式组成文件名并存储在文件系统的文件夹中

例如:

D:\Photos\69F.jpg

在整个应用程序中,将显示使用原始照片创建的不同大小的缩略图。由于缩略图很少发生更改,因此,如果有更改,缩略图将以以下格式创建并保存到同一文件系统文件夹中。(这意味着90%的时间,照片可以直接返回,无需任何图像处理)

  • 缩略图正常-D:\Photos\69F\u t.jpg
  • 缩略图-D:\Photos\69F_tm.jpg
所有照片都通过一个特殊的控制器(ImageController)访问,该控制器根据所需的缩略图类型有许多操作。例如,如果需要普通缩略图,则必须调用Thumb操作,然后确定缩略图是否存在,如果不存在,则在将其返回到浏览器之前创建并保存缩略图

\Image\Thumb\1695

应用程序运行的帐户对上面提到的文件夹具有完全访问权限,由于大多数情况下都有效,因此这不是权限问题

问题 我遇到的问题是,当调用获取缩略图时,应用程序会报告零星的错误。所有错误都遵循以下格式,但照片ID当然会更改(即,它发生在多张照片上):

进程无法访问文件“D:\Photos\69F_t.jpg”,因为另一进程正在使用该文件

或者

对路径“D:\Photos\69F_t.jpg”的访问被拒绝

上面的错误都源于最后一行的返回文件(…)Try…Catch:

Function Thumb(Optional ByVal id As Integer = Nothing)

    Dim thumbImage As WebImage
    Dim dimension As Integer = 200

    ' Create the image paths
    Dim imageOriginal As String = System.Configuration.ConfigurationManager.AppSettings("imageStore") + Hex(id) + ".jpg"
    Dim imageThumb As String = System.Configuration.ConfigurationManager.AppSettings("imageStore") + Hex(id) + "_t.jpg"

    ' If no image is found, return not found
    If FileIO.FileSystem.FileExists(imageOriginal) = False Then
        Return New HttpNotFoundResult
    End If

    ' If a thumbnail is present, check its validity and return it
    If FileIO.FileSystem.FileExists(imageThumb) = True Then
        thumbImage = New WebImage(imageThumb)

        ' If the dimensions are correct, simply return the image
        If thumbImage.Width = dimension Then
            thumbImage = Nothing
            Return File(imageThumb, System.Net.Mime.MediaTypeNames.Image.Jpeg)
        End If
    End If

    ' If we get this far, either the thumbnail is not the right size or does not exist!
    thumbImage = New WebImage(imageOriginal)

    ' First we must make the image a square
    If thumbImage.Height > thumbImage.Width Then
        ' Portrait
        Dim intPixelRemove As Integer

        ' Determine the amount to crop off the top and bottom
        intPixelRemove = (thumbImage.Height - thumbImage.Width) / 2
        thumbImage.Crop(intPixelRemove, 0, intPixelRemove, 0)
    Else
        ' Landscape
        Dim intPixelRemove As Integer

        ' Determine the amount to crop off the top and bottom
        intPixelRemove = (thumbImage.Width - thumbImage.Height) / 2
        thumbImage.Crop(0, intPixelRemove, 0, intPixelRemove)
    End If

    thumbImage.Resize(dimension + 2, dimension + 2, True, True)
    thumbImage.Crop(1, 1, 1, 1)
    thumbImage.Save(imageThumb)
    thumbImage = Nothing

    Try
        Return File(imageThumb, System.Net.Mime.MediaTypeNames.Image.Jpeg)
    Catch ex As Exception
        Return New HttpNotFoundResult
    End Try

End Function
问题 我很确定有什么东西使文件处于打开状态,因此不允许应用程序将文件返回到浏览器,但不管它是什么,它是非常零星的,因为这在90-95%的调用中都可以正常工作

这或问题是由于多人试图访问同一文件时并发造成的

  • 有人能发现这可能是什么原因吗
  • 如果多人同时访问同一张照片缩略图,我可能会遇到更多问题吗