서버

2013-02-12 2 views
0
내가 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 \ 가상 호스트 \ xx.com \ httpdocs \ 이미지 \ 섹션 \ 개발자 \ ClientLogo \ 원 - 작은 빈. 18x18.png '이 거부되었습니다. System.IO .__ Error.WinIOError System.IO.FileStream.Init에서 (INT32의 errorCode, 문자열 maybeFullPath) (문자열 경로, 대한 fileMode 모드, FileAccess 액세스, INT32 권한, 부울 useRights, 파일 공유 점유율, INT32 BUFFERSIZE, FileOptions 옵션에서 secAttrs을 SECURITY_ATTRIBUTES System.IO.FileStream.ctor (String 경로, FileMode 모드, FileAccess 액세스, FileShare 공유, Int32 bufferSize, FileOptions 옵션, String msgPath, 부울 bFromProxy)에서 System.IO.FileStream.ctor (String msgPath, 부울 bFromProxy, 부울 bFromProxy, 부울 useHongPath). xx.CorporateSite.Controllers.DeveloperController.ImagePath에서 System.Web.HttpPostedFileWrapper.SaveAs에서 System.Web.HttpPostedFile.SaveAs에서 IO.FileStream..ctor (문자열 경로, 대한 fileMode 모드) (문자열 파일 이름) (문자열 파일 이름) (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 System.IO.FileStream.Init에서 (INT32의 errorCode, 문자열 maybeFullPath) (문자열 경로, 대한 fileMode 모드, FileAccess 액세스, INT32 권한, 부울 useRights, 파일 공유 점유율, INT32 BUFFERSIZE, FileOptions 옵션에서 secAttrs을 SECURITY_ATTRIBUTES System.IO.FileStream.ctor (String 경로, FileMode 모드, FileAccess 액세스, FileShare 공유, Int32 bufferSize, FileOptions 옵션, String msgPath, 부울 bFromProxy)에서 System.IO.FileStream.ctor (String msgPath, 부울 bFromProxy, 부울 bFromProxy, 부울 useHongPath). xx.CorporateSite.Controllers.DeveloperController.ImagePath에서 System.Web.HttpPostedFileWrapper.SaveAs에서 System.Web.HttpPostedFile.SaveAs에서 IO.FileStream..ctor (문자열 경로, 대한 fileMode 모드) (문자열 파일 이름) (문자열 파일 이름) (HttpPostedFileBase imgfile)

내 로컬 호스트에서 권한을 변경하려고했지만 아무 것도 작동하지 않습니다 ... 제발 제안하십시오

답변

2

웹 응용 프로그램에 이미지를 저장하려는 위치에 글을 쓸 수있는 권한이 없습니다. 이것은 종종 모든 업로드 다음 코드에서 당신은 이미지가 될 곳의 경로를 식별하기 위해 그 구성 항목을 읽을 것

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

과 저장 폴더를 가리키는의 Web.config에 항목을 추가하여 처리 저장 : 다음

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

와이 디렉토리에 파일을 저장하기 위해, 당신은 웹 응용 프로그램으로 실행하는 사용자가 가지고있는 디렉토리에 대한 사용 권한을 쓰기 때문에 디렉토리에 대한 사용 권한을 설정해야합니다. 이렇게하면 웹 응용 프로그램의 파일을 해당 폴더에 쓸 수 있습니다.

이렇게하면 개발자가 파일을 저장할 위치를 지정하지 않습니다. 시스템 관리자는 파일이있는 위치와 웹 응용 프로그램을 지원하는 데 필요한 권한을 결정할 수 있습니다.

+0

이 값은 루트 경로가 필요하지만 내 로컬 및 서버 경로는 다릅니다. 그리고 저는 "~/Images/Sections/Developer/ClientLogo"처럼 줄 수는 없다고 생각합니다. – user207888

+0

'appSetting'은 문자열 값이기 때문에 필요한 경우 '~ \ Images \ ...'와 같은 상대 경로를 사용할 수 있습니다. 그러나 핵심은 web.config에 유지하여 하드 코딩되지 않도록하는 것입니다. 귀하의 환경이 확실하지 않지만 관리자가있는 경우 이미지 저장 위치 (예 : NAS 또는 다른 유형의 저장 장치)를 결정할 가능성이 있으며 해당 위치에서 적절한 권한 설정을 담당하게됩니다 서버. –

+1

내가 u 설정을 사용할 때. 오류가 발생 함 : System.Web.HttpException (0x80004005) : SaveAs 메서드가 루트 경로와 'abc.com/images/sections/Developer/ClientLogo/'경로를 요구하도록 구성되었습니다. over.png '; 뿌리가 없다 – user207888

0

폴더의 읽기 전용 속성이 활성화되어 있는지 확인하십시오. 이것이 Windows 컴퓨터 인 경우 폴더의 속성에서 선택을 취소하십시오.

되돌리기를 계속한다면 security-> advanced-> effective permissions-> select select ...-> 사용자 이름을 입력하고 전체 제어를 클릭 한 다음 확인을 클릭하여 폴더의 소유권을 가져야 할 수도 있습니다. 아웃.