我有一个程序,用户可以上传照片并将其存储在应用程序中(例如〜/ files / ac9ff990-273c-4fda-a51a-155c54db72ab / original / photo1.jpg)。稍后用户可以删除该文件。这工作正常,但是当删除所有子文件和文件夹时,告知级别文件夹(此GUID一个)仍然存在,并且我得到一个Unauthorizedaccessexception错误抛出。我留下了大量的空文件夹,在处理1000次上传时非常烦人。
我无法看到它是如何成为权限的东西,因为权限没什么奇怪的,我在桌面上和在实时服务器上遇到同样的问题。我已经开始关闭索引并试图给予“每个人”的许可而没有任何运气并将文件夹所有者设置为不同的用户而没有任何运气。 (我还没有在Windows资源管理器中打开该文件夹)
我无法看到这可能是一个权限问题,因为.NET应该使用相同的文件夹将其创建为删除它。有什么想法吗?
公共类FileManager { private static ILog log = LogManager.GetLogger(typeof(FileManager)); public enum 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以便不显示图像,则可以删除文件夹。对任何人都有意义吗?