请问如何在不获取此错误消息的情况下将文件在线上载到文件夹(拒绝访问路径。)

问题描述 投票:0回答:1

我正在尝试将图像文件在线上传到我的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:访问路径'C:\ inetpub \ wwwroot \ Portals \ 0 \ Images \ WebGeneralPropertiesFolder \ kansas-Image2.jpg'被拒绝。 ---> System.UnauthorizedAccessException:访问路径'C:\ inetpub \ wwwroot \ Portals \ 0 \ Images \ WebGeneralPropertiesFolder \ kansas-Image2.jpg'被拒绝。在System.IO .__ Error.WinIOError(Int32 errorCode,字符串System.IO.FileStream.Init(字符串路径,FileMode)中的maybeFullPath)模式,FileAccess访问权限,Int32权限,布尔useRights,FileShare份额,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTESsecAttrs,字符串msgPath,布尔bFromProxy,布尔useLongPath,System.IO.FileStream..ctor(布尔路径,FileMode)处的布尔值checkHost)模式,FileAccess访问权限,FileShare共享,Int32 bufferSize,FileOptions选项,字符串msgPath,布尔bFromProxy)位于System.IO.FileStream..ctor(字符串路径,FileMode模式)在System.Web.HttpPostedFile.SaveAs(字符串文件名)位于GeoscomTech.VenueHub.Property.View.UploadPropertyImage()在C:\ MyProjects \ www.venuehub.local.ng \ DesktopModules \ Property \ View.ascx.cs:line1717 ---内部异常堆栈跟踪结束---)

我正在使用Dnn内容管理,ASP.Net和C#语言构建应用程序。我将应用程序托管在Amazon Web Services上。使用MSSQL和IIS,在IIS中,我对iis Apppool \ DefaultApppool,网络服务,管理员拥有完全权限。仍然无法在线上传文件。

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

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

c# .net amazon-web-services dotnetnuke
1个回答
0
投票

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

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

if (!Directory.Exists(fileSavePath ))
    Directory.CreateDirectory(fileSavePath);
© www.soinside.com 2019 - 2024. All rights reserved.