简体   繁体   English

如何将文件在线上传到文件夹而不会出现“对路径的访问被拒绝”

[英]How Do I Upload a File To a Folder Online Without Getting "Access to the path is denied"

I am trying to upload an image file to my web application online.我正在尝试将图像文件在线上传到我的 Web 应用程序。 This is how am uploading images file online.这就是在线上传图像文件的方式。

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);
}

but instead am getting these error messages.而是收到这些错误消息。

(Error: Property is currently unavailable. DotNetNuke.Services.Exceptions.ModuleLoadException: Access to the path 'C:\\inetpub\\wwwroot\\Portals\\0\\Images\\WebGeneralPropertiesFolder\\kansas-Image2.jpg' is denied. ---> System.UnauthorizedAccessException: Access to the path 'C:\\inetpub\\wwwroot\\Portals\\0\\Images\\WebGeneralPropertiesFolder\\kansas-Image2.jpg' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) at System.Web.HttpPostedFile.SaveAs(String filename) at GeoscomTech.V (错误:属性当前不可用。DotNetNuke.Services.Exceptions.ModuleLoadException:拒绝访问路径“C:\\inetpub\\wwwroot\\Portals\\0\\Images\\WebGeneralPropertiesFolder\\kansas-Image2.jpg”。---> 系统.UnauthorizedAccessException: 访问路径 'C:\\inetpub\\wwwroot\\Portals\\0\\Images\\WebGeneralPropertiesFolder\\kansas-Image2.jpg' 被拒绝。在 System.IO.__Error.WinIOError(Int32 errorCode, String MaybeFullPath) 在 System. IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO。 FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) at System.Web。 GeoscomTech.V 上的 HttpPostedFile.SaveAs(String filename) enueHub.Property.View.UploadPropertyImage() in C:\\MyProjects\\www.venuehub.local.ng\\DesktopModules\\Property\\View.ascx.cs:line 1717 --- End of inner exception stack trace ---) enueHub.Property.View.UploadPropertyImage() in C:\\MyProjects\\www.venuehub.local.ng\\DesktopModules\\Property\\View.ascx.cs:line 1717 ---内部异常堆栈跟踪结束---)

I am building my application with Dnn Content Management, ASP.Net and C# language.我正在使用 Dnn 内容管理、ASP.Net 和 C# 语言构建我的应用程序。 I am hosting the application on Amazon Web Services.我在 Amazon Web Services 上托管该应用程序。 Using MSSQL and IIS, in IIS I have given full permission to iis Apppool\\DefaultApppool, Network Service, Administrator.使用 MSSQL 和 IIS,在 IIS 中,我已授予 iis Apppool\\DefaultApppool、网络服务、管理员的完全权限。 Still unable to upload file online.仍然无法在线上传文件。

This is my first time hosting on AWS.这是我第一次在 AWS 上托管。 Although i have hosted this kind of application on other hosting providers and it was working fine.虽然我已经在其他托管服务提供商上托管了这种应用程序,但它运行良好。

The funny thing is that it is working perfectly well on my local server.有趣的是,它在我的本地服务器上运行良好。 I would appreciate your feed back, thank you.我会很感激你的反馈,谢谢。

Are you sure that the directory in which you are uploading the image exists ?你确定你上传图片的目录存在吗? "C:\\inetpub\\wwwroot\\Portals\\0\\Images\\WebGeneralPropertiesFolder" "C:\\inetpub\\wwwroot\\Portals\\0\\Images\\WebGeneralPropertiesFolder"

Best to ensure this before using SaveAs method最好在使用 SaveAs 方法之前确保这一点

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM