2010-02-05 12 views
2

사용자가 인트라넷 웹 응용 프로그램에서 컴퓨터의 파일 경로를 지정할 수있는 방법을 제공해야하며, ASP : FileUpload 컨트롤은 좋은 대화 상자를 제공하지만, 파일의 내용. 파일 경로 만 필요하고 내용은 필요 없기 때문에이를 달성 할 수있는 또 다른 방법이 있습니까?파일 전송없이 FileUpload

답변

1

업데이트 : : 아래 코드는 IE에서 전체 파일 경로를 반환하지만 security reasons의 경우 파이어 폭스와 크롬 만 파일 이름을 제공합니다. 이것은 약간의 문제 일 수 있습니다 :)


나는 RaYell이 제시 한 해결책을 구현할 자유를 취했습니다.

Default.aspx를 (마크 업) : 여기 코드는

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SO._Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title></title> 
    <script type="text/javascript"> 
     function submitFormWithoutFile() { 
      var fileUpload = document.getElementById('<%= this.FileUpload1.ClientID %>'); 
      var filePath = document.getElementById('<%= this.FilePath.ClientID %>'); 
      filePath.value = fileUpload.value; 
      fileUpload.parentNode.removeChild(fileUpload); 
     } 
    </script> 
</head> 
<body> 

    <form id="form1" runat="server"> 
     <asp:HiddenField ID="FilePath" runat="server" /> 
     <asp:FileUpload ID="FileUpload1" runat="server" /> 

     <asp:Button ID="Button1" runat="server" Text="Do it!" OnClientClick="submitFormWithoutFile();" /> 
    </form> 
</body> 
</html> 

하여 default.aspx.cs (코드 숨김)

namespace SO 
{ 
    using System; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 

    public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (Page.IsPostBack) 
      { 
       // we have our file name but not the file 
       this.form1.Controls.Add(new Literal() { Text = this.FilePath.Value }); 
       System.Diagnostics.Debug.Assert(this.FileUpload1.PostedFile == null); 
      } 
     } 
    } 
} 
1

FileUpload 컨트롤을 추가 할 수 있습니다. 그런 다음 양식을 제출하기 전에 숨겨진 입력에 파일 경로를 복사하고 JavaScript로 FileUpload 컨트롤을 제거 할 수 있습니다.

저는 한번도 시도한 적이 없지만 제대로 작동합니다. 그래도 목적을 모르겠다.

+0

@RaYell 그는의 경로를 얻을 수 있습니다 자바 스크립트를 사용하는 파일 ... –

+0

나는 그가 파일에 대한 경로를 보유하고있는 파일 입력 값을 다른 (숨겨진 예를 들어) 서버에 전달 될 입력으로 복사 할 수 있다고 생각합니다. – RaYell