如何在没有"Access to the path is denied"的情况下在线将文件上传到文件夹



我正在尝试在线将图像文件上传到我的 Web 应用程序。这就是我在线上传图像文件的方式。

if (!FileUploadPropertyImage.HasFile)
{
Skin.AddModuleMessage(this, Localization.GetString("NoFileFound.ErrorMessage", LocalResourceFile), ModuleMessage.ModuleMessageType.RedError);
return;
}
string PictureFileTypeAccepted = "jpg,png,gif";
if (FileUploadPropertyImage.PostedFile.ContentLength <= maxFileSize * 1024)
{
string fileName = FileUploadPropertyImage.FileName.ToLower();
string extension = Path.GetExtension(fileName);
if (PictureFileTypeAccepted.Contains(extension.ToLower()))
{
System.Drawing.Bitmap objBmp = new System.Drawing.Bitmap(FileUploadPropertyImage.PostedFile.InputStream, false);
int imageWidth = objBmp.Width;
int imageHeight = objBmp.Height;
int minWidth = 1100;
int minHeight = 400;
if (imageWidth <= 2000 && imageHeight <= 800 && (imageWidth > minWidth && imageHeight > minHeight))
{
var mapPath = Server.MapPath($"\Portals\0\Images\WebGeneralPropertiesFolder\");
var fileSavePath = new DirectoryInfo(mapPath).FullName;
hiddenFieldPropertyImageUrl.Value = fileSavePath + "//" + FileUploadPropertyImage.FileName;
FileUploadPropertyImage.PostedFile.SaveAs(hiddenFieldPropertyImageUrl.Value);
hiddenFieldPropertImageName.Value =  FileUploadPropertyImage.FileName;
}
else
{
var wrongFileDimension = Localization.GetString("WrongFileDimension", LocalResourceFile);
wrongFileDimension = wrongFileDimension.Replace("#Height#", pictureHeight.ToString());
wrongFileDimension = wrongFileDimension.Replace("#Width#", pictureWidth.ToString());
Skin.AddModuleMessage(this, wrongFileDimension, ModuleMessage.ModuleMessageType.RedError);
}
}
else
{
Skin.AddModuleMessage(this, Localization.GetString("InvalidFileExtension.ErrorMessage", LocalResourceFile), ModuleMessage.ModuleMessageType.RedError);
}
}
else
{
Skin.AddModuleMessage(this, Localization.GetString("FileTooBig.ErrorMessage", LocalResourceFile),
ModuleMessage.ModuleMessageType.RedError);
}

但相反,我收到这些错误消息。

(错误:属性当前不可用。 DotNetNuke.Services.Exceptions.ModuleLoadException: Access to the path 'C:\inetpub\wwwroot\Portals\0\Images\WebGeneralPropertiesFolder\kansas-Image2.jpg' 被拒绝。--->系统.未经授权访问异常:访问路径 'C:\inetpub\wwwroot\Portals\0\Images\WebGeneralPropertiesFolder\kansas-Image2.jpg' 被拒绝。at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath( at System.IO.FileStream.Init(String path, FileMode 模式, 文件访问访问, Int32 权限, 布尔使用权限, 文件共享 共享、Int32 缓冲区大小、文件选项选项SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost( at System.IO.FileStream..ctor(字符串路径,文件模式 模式, 文件访问访问, 文件共享共享, Int32 缓冲区大小, FileOptions options, String msgPath, Boolean bFromProxy( at System.IO.FileStream..ctor(字符串路径,文件模式模式( at System.Web.HttpPostedFile.SaveAs(String filename( at GeoscomTech.VenueHub.Property.View.UploadPropertyImage(( in C:\MyProjects\www.venuehub.local.ng\DesktopModules\Property\View.ascx.cs:line 1717 --- 内部异常堆栈跟踪结束 ---(

我正在使用 Dnn 内容管理、ASP.Net 和 C# 语言构建我的应用程序。 我正在亚马逊云科技上托管该应用程序。 使用 MSSQL 和 IIS,在 IIS 中,我已经向 iis Apppool\DefaultApppool、Network Service、Administrator授予了完全权限。仍然无法在线上传文件。

这是我第一次在 AWS 上托管。尽管我已经在其他托管服务提供商上托管了这种应用程序并且它运行良好。

有趣的是,它在我的本地服务器上运行良好。感谢您的反馈,谢谢。

您确定要上传图像的目录存在吗? "C:\inetpub\wwwroot\Portals\0\Images\WebGeneralPropertiesFolder">

最好在使用 SaveAs 方法之前确保这一点

if (!Directory.Exists(fileSavePath ))
Directory.CreateDirectory(fileSavePath);

相关内容

最新更新