2011-11-29 5 views
12

파일을 업로드하고 이름을 아래로 변경하려고합니다. 파일 확장자를 가져와야합니다. 아래의 코드는 "Path"밑에 밑줄이 있는데, using 문이 없습니까? 또는 내가하고있는 것에 대한 올바른 구문은 무엇입니까?FileUpload는 파일 확장자를 가져옵니다.

if (FileUpload1.HasFile) 
try 
{ 
    var FileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName).Substring(1);      

    var newName = DateTime.Now.ToLongDateString(); 
    //Map path to folder 
    string realpath = Server.MapPath("Pictures\\") + Guid.NewGuid() + FileExtension;      

    FileUpload1.SaveAs(realpath); 

    Label1.Text = "File name: " + 
     FileUpload1.PostedFile.FileName + "<br>" + 
     FileUpload1.PostedFile.ContentLength + " kb<br>" + 
     "Content type: " + 
     FileUpload1.PostedFile.ContentType; 
} 
catch (Exception ex) 
{ 
    //Handle the error 
    throw ex; 
} 
else 
{ 
    Label1.Text = "You have not specified a file."; 
} 

답변

11

"경로"내가 using 문을 놓친 거지?

당신은 네임 스페이스

29
FileInfo fi = new FileInfo(fileName); 
string ext = fi.Extension; 
+0

대단히 감사합니다. –

1

귀하가 제공 한 코드가 잘 보이는 (그리고 내 컴퓨터에서 작동)의 목록에

using System.IO; 

을 추가해야합니다.

누락 된 것으로 보이는 유일한 것은 System.IO에 대한 사용 문입니다.

관련 문제