无法在服务器上上传文件

时间:2013-02-12 20:53:37

标签: c# asp.net-mvc io

我正在尝试上传一个image.it从我的本地主机上运行时效果很好但是当我发布它时它会从服务器抛出一个错误:

当我使用此代码时:

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)at System.IO.FileStream.Init(String path,FileMode mode,FileAccess access,Int32 rights,Boolean useRights,FileShare share,Int32 bufferSize,FileOptions options,SECURITY_ATTRIBUTES secAttrs) ,System msgPath,Boolean bFromProxy,Boolean useLongPath,Boolean checkHost)在System.IO.FileStream..ctor(String path,FileMode mode,FileAccess access,FileShare share,Int32 bufferSize,FileOptions options,String msgPath,Boolean bFromProxy)。 System.Web.HttpPostedFile.SaveAs(String filename)的System.Web.HttpPostedFileWrapper.SaveAs(String filename)位于xx.CorporateSite.Controllers.DeveloperController.ImagePath(HttpPostedFileBase imgfile)的IO.FileStream..ctor(String path,FileMode mode) )

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)at System.IO.FileStream.Init(String path,FileMode mode,FileAccess access,Int32 rights,Boolean useRights,FileShare share,Int32 bufferSize,FileOptions options,SECURITY_ATTRIBUTES secAttrs) ,System msgPath,Boolean bFromProxy,Boolean useLongPath,Boolean checkHost)在System.IO.FileStream..ctor(String path,FileMode mode,FileAccess access,FileShare share,Int32 bufferSize,FileOptions options,String msgPath,Boolean bFromProxy)。 System.Web.HttpPostedFile.SaveAs(String filename)的System.Web.HttpPostedFileWrapper.SaveAs(String filename)位于xx.CorporateSite.Controllers.DeveloperController.ImagePath(HttpPostedFileBase imgfile)的IO.FileStream..ctor(String path,FileMode mode) )

我试图从我的localhost更改权限,但没有任何工作......请建议我

1 个答案:

答案 0 :(得分:2)

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

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

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

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

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

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