2009-09-16 11 views
5

ASP.NET MVC로 작성된 파일 업로드 프로그램이 있습니다. 현재 로컬 개발 컴퓨터에 있습니다. 업로드 한 각 파일에 대한 링크를 생성하는 방법 (가능한 경우)을 클릭하여 항목을 표시/다운로드하는 방법을 알고 싶습니다.로컬 파일의 경로 경로 ASP.NET/MVC

현재 코드/파일 목록을 표시 핸들 태그 :

<table> 
    <tr> 
     <th></th> 
     <th> 
      Name 
     </th> 
     <th> 
      Length 
     </th> 
     <th></th> 
    </tr> 
    <% 
    var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "uploads"); 
    foreach (var file in Directory.GetFiles(path)) 
    { 
     var item = new FileInfo(file); 
    %> 
    <tr> 
     <td></td> 
     <td> 
      <%=Html.Encode(Path.GetFileName(item.Name))%> 
     </td> 
     <td> 
      <%=Html.Encode(item.Length >= 1024 ? item.Length/1024 + " kilobytes" : item.Length + " bytes")%> 
     </td> 
     <td> 
      // This is the line in question. Does not work as-is. 
      <a href="<%= item.FullName %>"><%= Html.Encode(Path.GetFileName(item.Name)) %></a> 
     </td> 
    </tr> 
    <% } %> 
</table> 

을 나는이 온라인 상태가 된 후에 주위의 파일 처리 코드를 변경해야합니다 상상,하지만 지금은이 충분하다. 제안도 환영합니다 :)

고마워!

답변

12

사용 Url.Content, 예컨대 :

<img src="<%= Url.Content("~/Content/UserImages/FileName.jpg") %>" /> 

물결 수단 "그 될 일이 어디 내 사이트의 루트를." Content에 파일을 넣을 필요는 없습니다. 당신은 당신이 당신의 위치 뿌리의 밑에 원한다 어디든지 그 (것)들을 둘 수있다.

1
<a href="<%= Url.Content(System.Web.VirtualPathUtility.ToAppRelative("~/" + file.Substring(AppDomain.CurrentDomain.BaseDirectory.Length))) %></a> 
+0

+1 (MVC3 - elmah.axd에 연결) 감사합니다! – ashes999

7

예는 ASP.NET 응용 프로그램에서 BaseDirectory에 해당하는 상응 HttpRuntime.AppDomainAppPath입니다. 그러나 Server.MapPath 메소드가 유용 할 수도 있습니다. HttpContext.Current.Server을 통해 서버 방법으로갑니다.

당신이보기에 이런 종류의 코드를 원한다고 말한 적이 있습니다. 그게 나에게 당신이 표시하고자하는 값 목록이 컨트롤러에 의해 생성되어야한다고 생각합니다.