2010-08-20 7 views
0

나는 이미지 업로드를 위해 파일 업로드 컨트롤을 사용하고 있습니다.iam이 조건을 확인하는 경우 Image.Width> 250 || Image.Height> 400이면 이미지의 크기가 변경됩니다. 하지만 오류가 발생했습니다 "SaveAs 메서드가 루트 경로를 요구하도록 구성되어 있고 'ProductImages/roman_sandals.jpg'경로가 루팅되지 않습니다."업로드하는 동안 이미지 크기 조정

ProductImages는 이미지를 저장하는 폴더입니다. 당신이 경우

HostingEnvironment.ApplicationPhysicalPath 

를 사용하여 이미지 경로와 결합 할 수

string strBigServerPath = AppHardcodeValue.productImgPath; 
      string strFileName = ""; 
      if (prodImg.HasFile) 
      { 
       strFileName = prodImg.PostedFile.FileName; 
       string uniqueNum = Convert.ToString(System.Guid.NewGuid()); 
       string shortFileName = System.IO.Path.GetFileName(strFileName); 
       string Extension = System.IO.Path.GetExtension(prodImg.FileName); 
       string newFileName = shortFileName; 
       prodImg.SaveAs(Server.MapPath(strBigServerPath + newFileName)); 
       using (System.Drawing.Image Img = 
        System.Drawing.Image.FromFile(Server.MapPath(strBigServerPath) + newFileName)) 
       { 
        if (Img.Width > 250 || Img.Height > 400) 
        { 
         Size MainSize = new Size(250, 400); 
         using (System.Drawing.Image ImgThnail = 
           new Bitmap(Img, MainSize.Width, MainSize.Height)) 
         { 
          prodImg.SaveAs(strBigServerPath + newFileName); 
         } 
        } 
        Img.Dispose(); 
       } 
       string ThumbnailPath = Server.MapPath(AppHardcodeValue.productThumbImgPath) + newFileName; 
       using (System.Drawing.Image Img = 
        System.Drawing.Image.FromFile(Server.MapPath(strBigServerPath) + newFileName)) 
       { 
        Size ThumbNailSize = new Size(50, 50); 

        using (System.Drawing.Image ImgThnail = 
         new Bitmap(Img, ThumbNailSize.Width, ThumbNailSize.Height)) 
        { 
         ImgThnail.Save(ThumbnailPath, Img.RawFormat); 
         ImgThnail.Dispose(); 
        } 
        Img.Dispose(); 
       } 

} 

답변

0

이 오류를주는 이유 사람이 찾을 수, 내 코드입니다. 예를 들어

:

Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "ProductImages/roman_sandals.jpg"); 

당신에게 뿌리 경로를 제공 할 것입니다. "ProductImages"폴더는 applications 디렉토리에 있어야합니다.

자세한 내용은 여기를 참조하십시오. http://msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment.applicationphysicalpath.aspx