我有一个程序,用户可以在其中上传照片并将其存储在应用程序中(例如 ~/files/ac9ff990-273c-4fda-a51a-155c54db72ab/original/photo1.jpg)。稍后用户可以删除该文件。这工作得很好,除了当所有子文件和文件夹都被删除时,告诉级别的文件夹(这个 GUID 文件夹)仍然存在并且我得到一个 Unauthorizedaccessexception 错误抛出。我留下了大量的空文件夹,这在处理 1000 次上传时真的很烦人。
我看不出它是如何成为权限的,因为权限并没有什么奇怪的,而且我在桌面上遇到的问题与在实时服务器上的问题相同。我已关闭索引并尝试在没有任何运气的情况下授予“所有人”权限,并将文件夹所有者设置为不同的用户而没有任何运气。(我也没有在 Windows 资源管理器中打开该文件夹)
我看不出这可能是一个权限问题,因为 .NET 应该使用相同的文件夹来创建它作为删除它。有任何想法吗?
公共类 FileManager { 私有静态 ILog 日志 = LogManager.GetLogger(typeof(FileManager)); 公共枚举 UPLOAD_TYPE : int { ORIGINAL_FILE = 1, PROCESSED_FILE = 2, ANCILLARY_FILE = 3 }
private const string userFilesFolder = "~/files/";
private const string originalSubFolder = "original/";
private const string processedSubFolder = "processed/";
private const string ancillarySubFolder = "ancillary/";
internal static void DeleteJobItemFiles(Guid jobItemId)
{
if (jobItemId == Guid.Empty)
throw new ApplicationException("jobItemId is Empty");
try
{
var directory = GetDirectory(jobItemId);
string routePath = HttpContext.Current.Server.MapPath(HttpUtility.UrlDecode(directory.AbsolutePath));
if (Directory.Exists(routePath))
Directory.Delete(routePath, true);
}
catch (HttpException exception)
{
throw new UploadManagerException(ERROR_CODE.HTTP_CONTEXT_NULL, exception);
}
catch (Exception exception)
{
throw new UploadManagerException(ERROR_CODE.GENERAL_FILE, exception);
}
}
internal static string SaveFile(Guid jobItemId, Stream inputStream, UPLOAD_TYPE uploadType, string fileName)
{
if (jobItemId == Guid.Empty)
throw new ArgumentException("jobItemId is Empty");
if (inputStream == null)
throw new ArgumentNullException("inputStream");
if (fileName == null)
throw new ArgumentNullException("fileName");
if (inputStream.CanSeek)
inputStream.Position = 0;
Regex rgx = new Regex("[^a-zA-Z0-9 -_\\.]");
fileName = rgx.Replace(fileName, "");
fileName = fileName.Replace(" ", "");
var directoryPathUri = GetDirectory(jobItemId, uploadType);
// New file Uri
var fileUri = new Uri(directoryPathUri, fileName);
try
{
string localDirectoryPath = HttpContext.Current.Server.MapPath(HttpUtility.UrlDecode(directoryPathUri.AbsolutePath));
string localFilePath = HttpContext.Current.Server.MapPath(HttpUtility.UrlDecode(fileUri.AbsolutePath));
if (!Directory.Exists(localDirectoryPath))
Directory.CreateDirectory(localDirectoryPath);
using (FileStream outputStream = File.Create(localFilePath))
{
Byte[] buffer = new Byte[256];
inputStream.Position = 0;
int bytesRead;
do
{
bytesRead = inputStream.Read(buffer, 0, buffer.Length);
if (bytesRead > 0)
outputStream.Write(buffer, 0, bytesRead);
}
while (bytesRead > 0);
}
}
catch (IOException exception)
{
log.Error("Unexpected exception in FileManager.SaveFile", exception);
throw new UploadManagerException(ERROR_CODE.FILE_SAVE, exception);
}
catch (HttpException exception)
{
throw new UploadManagerException(ERROR_CODE.HTTP_CONTEXT_NULL, exception);
}
catch (Exception exception)
{
throw new UploadManagerException(ERROR_CODE.GENERAL_FILE, exception);
}
var rootUri = GetDirectory();
return VirtualPathUtility.ToAppRelative(fileUri.AbsolutePath);
}
private static Uri GetDirectory()
{
// Root of site
try
{
return new Uri(HttpContext.Current.Request.Url, HttpContext.Current.Request.ApplicationPath);
}
catch (HttpException exception)
{
throw new UploadManagerException(ERROR_CODE.HTTP_CONTEXT_NULL, exception);
}
}
private static Uri GetDirectory(Guid jobItemId)
{
if (jobItemId == Guid.Empty)
throw new ArgumentException("jobItemId is Empty");
var rootUri = GetDirectory();
//Folder where all files are stored
var userFilesUri = new Uri(rootUri, VirtualPathUtility.ToAbsolute(userFilesFolder));
//Folder for job
return new Uri(userFilesUri, jobItemId.ToString() + "/");
}
private static Uri GetDirectory(Guid jobItemId, UPLOAD_TYPE uploadType)
{
if (jobItemId == Guid.Empty)
throw new ArgumentException("jobItemId is Empty");
string subFolder;
switch (uploadType)
{
case UPLOAD_TYPE.ORIGINAL_FILE:
subFolder = originalSubFolder;
break;
case UPLOAD_TYPE.PROCESSED_FILE:
subFolder = processedSubFolder;
break;
case UPLOAD_TYPE.ANCILLARY_FILE:
subFolder = ancillarySubFolder;
break;
default:
throw new NotImplementedException();
}
//Folder for file
return new Uri(GetDirectory(jobItemId), subFolder);
}
}
重要更新:这似乎是由 IIS 锁定文件或类似原因引起的。我试图删除的图像显示在页面上。当 WCF 调用来删除页面时,无法删除文件夹。如果我编辑 HTML 以便不显示图像,则可以删除文件夹。对任何人都有意义吗?