2012-11-15 3 views
1

PDF/Word/Excel 파일을 표시하는 Asp.NET 사이트이지만 사용자가 표시 대신 링크를 통해 파일을 요청한 경우 파일의 위치를 ​​숨기고 싶습니다. 경로는 파일 이름을 열어 봤지만 다른 사이트에서는 다른 게시물을 보았습니다. 그러나 오래된 사이트이므로 wok을하지 마십시오.Asp.Net 파일의 URL을 숨기기

도움을 주시면 감사하겠습니다. 이 IHttpHandler를 구현하는 사용자 정의 .ASHX 핸들러를 작성하는 것입니다 주위에

감사

+0

당신이 시도한 오드? –

답변

1

한 가지 방법은 얻을 수 있습니다. ASP.NET ASHX Handler

일부 ASP

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest 

     Dim inline As Boolean = Boolean.Parse(context.Request.QueryString("Inline")) 
     Dim fileName As String = context.Request.QueryString("fileName") 
     If (fileName.Contains("\")) Then Throw New Exception(String.Format("Invalid filename {0}. Looks like a path was attempted", fileName)) 


     Dim filePath = ConfigurationManager.AppSettings("FileDirectory") + "\" + fileName 
     With context.Response 
      .Buffer = True 
      .Clear() 

      If inline Then 
       .AddHeader("content-disposition", "inline; ; filename=" & IO.Path.GetFileName(filePath)) 
      Else 
       .AddHeader("content-disposition", "attachment; ; filename=" & IO.Path.GetFileName(filePath)) 
      End If 

      .WriteFile(filePath) 

      If fileName.ToUpper.EndsWith(".PDF") Then 
       .ContentType = "application/pdf" 
      ElseIf fileName.EndsWith(".htm") Or fileName.EndsWith(".html") Then 
       .ContentType = "text/html" 
      ElseIf fileName.EndsWith(".tif") Then 
       .ContentType = "image/tiff" 
      ElseIf fileName.EndsWith(".jpeg") Or fileName.EndsWith(".jpg") Then 
       .ContentType = "image/jpeg" 
      End If 

      .End() 
     End With 
2

사용 :

응답에서이 ProcessRequest (..) 메소드, 및 파이프 파일을 구현

은 (여기에 내가 잠시 다시 쓴 응용 프로그램의 예입니다. NET 파일은 동적으로 생성되며 C# 코드 또는 디스크 리소스로 생성되며 웹 양식이 필요하지 않으며 대신 ASHX 제네릭 처리기가 이상적입니다. 쿼리 문자열에서 이미지를 동적으로 반환하거나 XML을 작성하거나 기타 데이터입니다.

관련 문제