在Net4India上传文件时出错

时间:2014-07-31 09:17:32

标签: c# asp.net .net image

我是asp.net的新手。我正在尝试将图像从子目录上传到root。它在调试代码时工作正常。但是当我在IIS上传时,它会给出一个

Access to the path 'C:\inetpub\wwwroot\APBooksHosting\HomeImage\Desert.jpg' is denied.

我的目录结构:

-ROOT-books-users-create-HomePage.aspx(我尝试上传文件的页面)

-ROOT-HomeImage(“这是我要上传文件的文件夹”)

我正在使用此代码上传文件

namespace WebApplication1.books.users.Create
{
 public partial class Homeimage : System.Web.UI.Page
 {

    public void upload image()
    {
       if (homeimguploader.HasFile)
       {


                String path = Server.MapPath("~/HomeImage/");
                String ext = Path.GetExtension(homeimguploader.FileName);

                if (ext == ".jpg" || ext == ".png" || ext == ".gif")
                {

                    homeimguploader.SaveAs(path + homeimguploader.FileName);
                    homeimage = homeimguploader.FileName;
                }
       }
    }
 }
}

1 个答案:

答案 0 :(得分:0)

对于权限问题,您可以使用以下代码:

// Get full permissons 
var permission = new FileIOPermission(FileIOPermissionAccess.Write, "Full Physical Path Here");
var permissionSet = new PermissionSet(PermissionState.None);
permissionSet.AddPermission(permission);
if (permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet))
{
      //your code here
}