locked
Upload file from client to server RRS feed

  • Question

  • User747530935 posted

     Hello Forum,

    This is my first post and being a beginner in ASP.Net needs your support desperately and urgently.

    I need to upload a file browsed by a user to my server.I am using a HTML page and the <input type="file" id="file1" name="file"> is mentioned in the HTML page. On the click of submit on HTMl page i am using method="post" action="form.aspx". Then from the form.aspx i need to upload the selected file to the server. I am using the following code in form.aspx:

     if ((Request.Form["file1"]).PostedFile != null)
     {

       file1.PostedFile.SaveAs("D:\\Inetpub\\Wwwroot\\sitename\\dotnet\\attachments\\test.txt");
     }

    From my understanding the above code should upload the file selected by user in "file1" file ope dialouge box should be copied to the server in the following path ("D:\\Inetpub\\Wwwroot\\sitename\\dotnet\\attachments" with name test.txt.

    But when i run this code it gives the following error message"CS0117: 'string' does not contain a definition for 'PostedFile'". Please let me know how to solve this issue.

    An urgent/quick help is highly appreciated.

    Many Thanks.

    Tuesday, October 6, 2009 5:01 PM

Answers

  • User-986547226 posted

    Here is a sample you can refer

    http://www.4guysfromrolla.com/webtech/091201-1.shtml 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 6, 2009 6:02 PM
  • User1452680134 posted

    protected void Submit1_ServerClick(object sender, EventArgs e)
        {
            if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
            {

                string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
              
                Label1.Text = "file name" + fn;
                string SaveLocation = Server.MapPath("Data") + "\\" + fn;
                try
                {
                    File1.PostedFile.SaveAs(SaveLocation);
                    Response.Write("The file has been uploaded.");
                }
                catch (Exception ex)
                {
                    Response.Write("Error: " + ex.Message);

                }
            }
            else
            {
                Response.Write("Please select a file to upload.");
            }
            rebind();
        }
        private void rebind()
        {
            DirectoryInfo mydir = new DirectoryInfo(Server.MapPath("Data"));
            articleList.DataSource = mydir.GetFiles();
            articleList.DataBind();
        }

    ....


    And this in aspx file


    <body>
        <form id="Form1" method="post" enctype="multipart/form-data" runat="server">
    <INPUT type=file id=File1 name=File1 runat="server" />
    <br>
    <input type="submit" id="Submit1" value="Upload" runat="server" onserverclick="Submit1_ServerClick" />
        <div>
        <a href="Data/DELTA.MPG">Play a video file</a>&nbsp;
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <asp:DataGrid runat="server" id="articleList" AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#eeeeee" HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White" HeaderStyle-Font-Size="15pt" HeaderStyle-Font-Bold="True">
      <Columns>
        <asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name" DataNavigateUrlFormatString="Data/{0}" HeaderText="File Name" />
        <asp:BoundColumn DataField="LastWriteTime" HeaderText="Last Write Time" ItemStyle-HorizontalAlign="Center" DataFormatString="{0:d}" />
        <asp:BoundColumn DataField="Length" HeaderText="File Size" ItemStyle-HorizontalAlign="Right" DataFormatString="{0:#,### bytes}" />
      </Columns>
    </asp:DataGrid>  

        </div>
        </form>
    </body>


    files are uploaded to Data folder (so create one in the project).


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2009 8:07 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2009 8:10 AM
  • User604186779 posted

     if ((Request.Form["file1"]).PostedFile != null)
     {

       file1.PostedFile.SaveAs("D:\\Inetpub\\Wwwroot\\sitename\\dotnet\\attachments\\test.txt");
     }

     

    Try this

    for (int i = 0; i < Request.Files.Count; i++) 
    
        { 
    
            HttpPostedFile PostedFile = Request.Files[i]; 
    
            if (PostedFile.ContentLength > 0) 
    
            { 
    
                string FileName = System.IO.Path.GetFileName(PostedFile.FileName); 
    
                PostedFile.SaveAs(Server.MapPath("Files\\") + FileName); 
    
            } 
    
        } 
    


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2009 8:31 AM

All replies

  • User-986547226 posted

    Here is a sample you can refer

    http://www.4guysfromrolla.com/webtech/091201-1.shtml 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 6, 2009 6:02 PM
  • User-311990318 posted

    The Request.Form collection contains all the posted form data. The Request.Files collection contains all the posted files, so you should use this collection instead- e.g:

                if (Request.Files.Count > 0)
                {
                    foreach (HttpPostedFile file in Request.Files)
                    {
                        if (file.ContentLength > 0)
                        {
                            //save it
                        }
                    }
                }



    Wednesday, October 7, 2009 7:42 AM
  • User1452680134 posted

    protected void Submit1_ServerClick(object sender, EventArgs e)
        {
            if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
            {

                string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
              
                Label1.Text = "file name" + fn;
                string SaveLocation = Server.MapPath("Data") + "\\" + fn;
                try
                {
                    File1.PostedFile.SaveAs(SaveLocation);
                    Response.Write("The file has been uploaded.");
                }
                catch (Exception ex)
                {
                    Response.Write("Error: " + ex.Message);

                }
            }
            else
            {
                Response.Write("Please select a file to upload.");
            }
            rebind();
        }
        private void rebind()
        {
            DirectoryInfo mydir = new DirectoryInfo(Server.MapPath("Data"));
            articleList.DataSource = mydir.GetFiles();
            articleList.DataBind();
        }

    ....


    And this in aspx file


    <body>
        <form id="Form1" method="post" enctype="multipart/form-data" runat="server">
    <INPUT type=file id=File1 name=File1 runat="server" />
    <br>
    <input type="submit" id="Submit1" value="Upload" runat="server" onserverclick="Submit1_ServerClick" />
        <div>
        <a href="Data/DELTA.MPG">Play a video file</a>&nbsp;
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <asp:DataGrid runat="server" id="articleList" AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#eeeeee" HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White" HeaderStyle-Font-Size="15pt" HeaderStyle-Font-Bold="True">
      <Columns>
        <asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name" DataNavigateUrlFormatString="Data/{0}" HeaderText="File Name" />
        <asp:BoundColumn DataField="LastWriteTime" HeaderText="Last Write Time" ItemStyle-HorizontalAlign="Center" DataFormatString="{0:d}" />
        <asp:BoundColumn DataField="Length" HeaderText="File Size" ItemStyle-HorizontalAlign="Right" DataFormatString="{0:#,### bytes}" />
      </Columns>
    </asp:DataGrid>  

        </div>
        </form>
    </body>


    files are uploaded to Data folder (so create one in the project).


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2009 8:07 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2009 8:10 AM
  • User604186779 posted

     if ((Request.Form["file1"]).PostedFile != null)
     {

       file1.PostedFile.SaveAs("D:\\Inetpub\\Wwwroot\\sitename\\dotnet\\attachments\\test.txt");
     }

     

    Try this

    for (int i = 0; i < Request.Files.Count; i++) 
    
        { 
    
            HttpPostedFile PostedFile = Request.Files[i]; 
    
            if (PostedFile.ContentLength > 0) 
    
            { 
    
                string FileName = System.IO.Path.GetFileName(PostedFile.FileName); 
    
                PostedFile.SaveAs(Server.MapPath("Files\\") + FileName); 
    
            } 
    
        } 
    


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2009 8:31 AM