【问题标题】:asp.net browse for fileasp.net 浏览文件
【发布时间】:2012-03-24 08:00:19
【问题描述】:

我在我的 asp.net 网页中有一个方法可以将 csv 文件转储到我的 gridview 中,但我想包含一个对话框供用户浏览并从他们的 PC 中选择一个 csv 文件以导入并获取该文件名和输入我的 csv 导入方法的路径信息,以便它可以对文件进行操作。有没有简单的方法可以做到这一点?

【问题讨论】:

  • 我有点困惑,您希望您网站的用户能够上传 csv 文件吗?
  • html 表单(multipart/form-data 类型)内的任何 html 输入标签(类型=文件)都可以。
    。用户将按下浏览按钮(来自文件输入标签)。然后当用户按下提交按钮时,csv文件将被发送到asp.net文件集合。

标签: asp.net vb.net openfiledialog


【解决方案1】:

你需要这个类 System.Web.UI.HtmlControls.HtmlInputFile

来自 MSDN:

使用 HtmlInputFile 服务器控件来处理上传二进制文件或 从浏览器客户端到服务器的文本文件。文件上传适用于 Microsoft Internet Explorer 3.02 或更高版本。

更新:这是来自 MSDN (.net v1.1) 的工作代码示例

<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
 <head>

    <script language="VB" runat="server">
       Sub Button1_Click(Source As Object, e As EventArgs)

            If Text1.Value = "" Then
                Span1.InnerHtml = "Error: you must enter a file name"
                Return
            End If

            If Not (File1.PostedFile Is Nothing) Then
                Try
                    File1.PostedFile.SaveAs(("c:\temp\" & Text1.Value))
                    Span1.InnerHtml = "File uploaded successfully to <b>c:\temp\" & _
                                      Text1.Value & "</b> on the Web server"
                Catch exc As Exception
                    Span1.InnerHtml = "Error saving file <b>c:\temp\" & _
                                      Text1.Value & "</b><br>" & exc.ToString()
                End Try
            End If
        End Sub 'Button1_Click 
    </script>

 </head>
 <body>

    <h3>HtmlInputFile Sample</h3>

    <form enctype="multipart/form-data" runat="server">

       Select File to Upload: 
       <input id="File1" 
              type="file" 
              runat="server">

       <p>
       Save as filename (no path): 
       <input id="Text1" 
              type="text" 
              runat="server">

       <p>
       <span id=Span1 
             style="font: 8pt verdana;" 
             runat="server" />

       <p>
       <input type=button 
              id="Button1" 
              value="Upload" 
              OnServerClick="Button1_Click" 
              runat="server">

    </form>

 </body>
 </html>

【讨论】:

  • 感谢您的回复。 - 我现有的代码已经从文件中抓取了 csv 数据 - 它只需要知道文件在用户计算机上的位置 - 因此是浏览要求。我真的不需要文件本身。
【解决方案2】:

到目前为止,这里的每个人的答案似乎都是正确的。如果您想坚持使用服务器端控件,另一种选择是使用 ASP.NET FileUpload control

下面是我无耻从here偷来的控件的使用示例:

<%@ Page Language="C#" %>

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

<script runat="server">

  protected void UploadButton_Click(object sender, EventArgs e)
  {
    // Specify the path on the server to
    // save the uploaded file to.
    String savePath = @"c:\temp\uploads\";

    // Before attempting to perform operations
    // on the file, verify that the FileUpload 
    // control contains a file.
    if (FileUpload1.HasFile)
    {
      // Get the name of the file to upload.
      String fileName = FileUpload1.FileName;

      // Append the name of the file to upload to the path.
      savePath += fileName;


      // Call the SaveAs method to save the 
      // uploaded file to the specified path.
      // This example does not perform all
      // the necessary error checking.               
      // If a file with the same name
      // already exists in the specified path,  
      // the uploaded file overwrites it.
      FileUpload1.SaveAs(savePath);

      // Notify the user of the name of the file
      // was saved under.
      UploadStatusLabel.Text = "Your file was saved as " + fileName;
    }
    else
    {      
      // Notify the user that a file was not uploaded.
      UploadStatusLabel.Text = "You did not specify a file to upload.";
    }

  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>FileUpload Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <h4>Select a file to upload:</h4>

       <asp:FileUpload id="FileUpload1"                 
           runat="server">
       </asp:FileUpload>

       <br /><br />

       <asp:Button id="UploadButton" 
           Text="Upload file"
           OnClick="UploadButton_Click"
           runat="server">
       </asp:Button>    

       <hr />

       <asp:Label id="UploadStatusLabel"
           runat="server">
       </asp:Label>        
    </div>
    </form>
</body>
</html>

【讨论】:

  • -1 您的代码在 Visual Studio 2010 中编译失败。有 42 个错误。
  • 我想你不知道你在说什么。该示例并不意味着是复制粘贴工作。这是一个直接取自 MSDN 的示例。您是否也在期待一个 csproj 文件?
  • @SquidScareMe 太聪明了,不得不提的是您的代码示例是用 C# 编写的,但问题是关于 VB.NET。 OP可能无法完全理解。
  • @Andrew,这是一个很好的解释,我应该考虑到这一点。谢谢。我也是个聪明人,为自己的反应感到羞耻。
  • 你的回复可能有点聪明,但至少没有人可以责怪你。如果唐恩自己解释,那就很容易解决了。
【解决方案3】:

对于 ASP.NET(即 Web 应用程序而不是桌面),将文件上传到 Web 服务器(在这种情况下可能不需要,但最初的问题在这一点上并不清楚),我建议从 VS 工具箱的 asp:fileupload 控件开始。

【讨论】:

    猜你喜欢
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    相关资源
    最近更新 更多