【问题标题】:Not able to upload a file on server无法在服务器上上传文件
【发布时间】:2013-01-28 07:03:44
【问题描述】:

我正在尝试上传一张图片。当我从本地主机上传时它工作正常,但是当我发布它时它会从服务器抛出一个错误:

当我使用这段代码时:

public string ImagePath(HttpPostedFileBase imgfile)
        {
            var path = "";
            // code for saving the image file to a physical location.
            var fileName = Path.GetFileName(imgfile.FileName);


            path = Path.Combine(HttpContext.Server.MapPath("~/Images/Sections/Developer/ClientLogo"), fileName);

            string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(imgfile.FileName);
            int iteration = 1;
            while (System.IO.File.Exists((path)))
            {
                fileName = string.Concat(fileNameWithoutExtension, "-", iteration, System.IO.Path.GetExtension(imgfile.FileName));

                path = Path.Combine(HttpContext.Server.MapPath("~/Images/Sections/Developer/ClientLogo"), fileName);
                iteration++;
            }
            imgfile.SaveAs(path);
            // prepare a relative path to be stored in the database and used to display later on.
            path = Url.Content(Path.Combine("~/Images/Sections/Developer/ClientLogo", fileName));
            return path;
        }

错误是

System.UnauthorizedAccessException:对路径“D:\InetPub\vhosts\xx.com\httpdocs\Images\Sections\Developer\ClientLogo\circle-small-empty.18x18.png”的访问被拒绝。在 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.HttpPostedFile.SaveAs(String filename) at System.Web.HttpPostedFileWrapper.SaveAs(String filename) at xx.CorporateSite.Controllers.DeveloperController.ImagePath(HttpPostedFileBase imgfile )

And when I use Server.MapPath instead of HttpContext.Server.MapPath it throw different error:

错误是:

System.IO.DirectoryNotFoundException:找不到路径“D:\InetPub\vhosts\xx.com\httpdocs\Images\Sections\Developer\ClientLogo\demo.png”的一部分。在 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.HttpPostedFile.SaveAs(String filename) at System.Web.HttpPostedFileWrapper.SaveAs(String filename) at xx.CorporateSite.Controllers.DeveloperController.ImagePath(HttpPostedFileBase imgfile )

我尝试从本地主机更改权限,但没有任何效果...请给我一些建议

【问题讨论】:

    标签: c# asp.net-mvc io


    【解决方案1】:

    您的网络应用程序无权写入您尝试保存图像的位置。这通常通过在 web.config 中添加一个条目来处理,该条目指向保存所有上传的文件夹

    <appSettings>
      ...
      <add key="uploadPath" value="C:\Uploads"/>
      ...
    </appSettings>
    

    然后在您的代码中,您将读取该配置条目以识别将保存图像的路径:

    ....
    string path = ConfigurationManager.AppSettings["uploadPath"];
    string filePath = Path.Combine(path, fileName);
    ....
    

    然后为了将文件保存到此目录,您需要设置该目录的权限,以便运行 Web 应用程序的用户对该目录具有写入权限。这将允许您将文件从 Web 应用程序写入该文件夹。

    这样,作为开发人员的您无需指定文件的去向。系统管理员可以决定文件的去向,以及支持您的 Web 应用程序所需的权限。

    【讨论】:

    • 。此值需要根路径,但我的本地和服务器路径不同。而且我认为我不能像这样给出“~/Images/Sections/Developer/ClientLogo”
    • appSetting 只是一个字符串值,因此您可以根据需要使用像 '~\Images\...' 这样的相对路径。但是,关键是将它保存在您的 web.config 中,这样它就不会被硬编码。我不确定你的环境,但如果你有管理员,他们可能会决定图像应该存储在哪里(可能在 NAS 或其他类型的存储上),并且他们将负责适当地设置权限服务器。
    • 当我使用您提供的设置时。我收到一个错误:System.Web.HttpException (0x80004005): SaveAs 方法配置为需要根路径,并且路径'abc.com/images /sections/Developer/ClientLogo/over.png';没有root
    • 这个解决方案是否适用于任何人,请让我知道我也陷入了这个困境。
    【解决方案2】:

    检查文件夹的只读属性是否处于活动状态。如果这是一台windows机器,在文件夹的属性中取消勾选。

    如果它不断恢复,您可能需要通过转到安全性->高级->有效权限->单击选择...->输入您的用户名并检查完全控制->然后单击确定来获取文件夹的所有权.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-17
      • 2013-07-23
      • 1970-01-01
      • 2014-04-04
      • 2013-07-09
      • 1970-01-01
      相关资源
      最近更新 更多