0

I have a method in my asp.net webpage to dump a csv file into my gridview but i wanted to include a dialog box for the user to browse and select a csv file from their PC to import and grab that filename name and path info to feed into my csv import method so it can act upon the file. Is there a simple way to do this?

3
  • I'm a bit confused, do you want the users of your web site to able to upload csv files? Commented Mar 24, 2012 at 8:16
  • ok, then try this techrepublic.com/blog/programming-and-development/… and this brangle.com/wordpress/2009/09/… Commented Mar 24, 2012 at 9:03
  • any html input tag (of type=file) inside a html form (of multipart/form-data type) will do . <form action="your.aspx" method="post" enctype="multipart/form-data"> <input type="file" name="sumbittedCSV" /> <input type="submit"/> </form>. The user will press the browse button (from the file input tag) . Then when user press the submit button, csv files will be send to the asp.net files collection . Commented Mar 24, 2012 at 9:21

3 Answers 3

1

You need this class System.Web.UI.HtmlControls.HtmlInputFile

From MSDN:

Use the HtmlInputFile server control to handle uploading binary or text files from a browser client to the server. File upload works with Microsoft Internet Explorer version 3.02 or later.

UPDATE: Here is working code example from 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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your replies. - My existing code already grabs the csv data out of the file - it just needs to know where the file is on the users computer - hence the browse requirement. I dont really need the file itself.
0

So far everyone here seems correct in their answers. One other option is to use the ASP.NET FileUpload control if you want to stick to server-side controls.

Here is an example of how to use the control that I shamelessly stole from 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>

5 Comments

-1 Your code failed to compile in Visual Studio 2010. There were 42 errors.
I don't think you know what you're talking about. That example wasn't meant to be a copy-paste job. It was an example TAKEN DIRECTLY FROM MSDN. Were you expecting a csproj file, too?
@SquidScareMe What Down was being too smart-ass to mention was that your code sample is in C#, but the question is about VB.NET. The OP may or may not be able to fully comprehend.
@Andrew, that's a good explanation and I should have considered that. Thanks. I was a smart-ass as well and ashamed of how I responded.
You were maybe a little bit smart with your reply, but no one could blame you in the least. If Down had explained himself, it would have been easy to address.
0

For ASP.NET (i.e. web application rather than desktop), to upload a file to the webserver (which it turns out may not be required in this case but the original question was not clear on that point), I would suggest starting with an asp:fileupload control for the VS toolbox.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.