how to upload file via c# code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thevinh92
    New Member
    • Aug 2012
    • 10

    #1

    how to upload file via c# code

    Hi, i want to create a web page that allow to be upload.
    And my question is: without using the web browser I want to use something in System.Net such as HttpWebRequest or WebClient or some kind to programmaticall y upload a file along with sending the checkbox to that upload page.
    And i have just read this link:


    and i don't know the server code clearly. Yes, please, tell me the server code.
    sorry for bad english.
    Last edited by Niheel; Aug 10 '12, 01:42 PM. Reason: made link clickable
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I don't understand what you are trying to do.

    Do you want to upload a file to the server through a webpage?

    Or do you want to create a desktop application that uploads a webpage-file to a server?

    -Frinny

    Comment

    • thevinh92
      New Member
      • Aug 2012
      • 10

      #3
      thanks for your reply!
      hmm, i'm trying to create a application that upload Images to a local asp.net web page!
      And, i don't know how to code in the asp.net project (the server?)
      Please, take a look in the link I gave above for more information!

      Comment

      • ariful alam
        New Member
        • Jan 2011
        • 185

        #4
        @thevinh92,

        What you added above in your first post is a server side webpage created using ASP.net technology. To create a ASP.net webpage you can use VB.net or C#.net language.

        Now, you can upload image in your web server using two technique. one is in your web server's file system. and another is in your web server's database system.

        Now, question is what you need? to sample you a ASP.net image/file upload code or to describe you the code that you linked in your first post.

        hope it's help you. :)

        Comment

        • thevinh92
          New Member
          • Aug 2012
          • 10

          #5
          Hi, ariful alam! thanks for your reply!
          yes, i need your help in describe the code that i linked in my first (all of the server code and client code, but the server code is the most). And i'll very happy if you show me another asp.net image/file upload code!

          Comment

          • ariful alam
            New Member
            • Jan 2011
            • 185

            #6
            I can describe another ASP.net image/file upload code.

            but you have to tell me, do you have any idea about server side code and ASP.net technology? Do you have Visual Studio.net? i use Visual Studio.net 2010 for creating ASP.net projects.

            Comment

            • thevinh92
              New Member
              • Aug 2012
              • 10

              #7
              yes,i have Visual Studio.net 2010!

              Comment

              • ariful alam
                New Member
                • Jan 2011
                • 185

                #8
                Ok,

                Start a ASP.net/web project (New web site) (ASP.net empty web site) in your Visual Studio.net 2010 using Visual C#.

                Now a new web form from Website->Add New Item..

                This will add a default.aspx page in your project. Now Add/drag a FileUpload control and a Button control from the Toolbox in your default.aspx page.

                Code:
                <asp:FileUpload ID="FileUpload1" runat="server" />
                Code:
                <asp:Button ID="Button1" runat="server" Text="Button" />
                Now double click the Button. That will open up default.aspx.cs file to write code for uploading an image.

                write the following in your button's event handler.

                Code:
                string savePath = Server.MapPath("images\\");
                        if (FileUpload1.PostedFile.ContentLength != 0)
                        {
                            FileUpload1.SaveAs(savePath + FileUpload1.FileName);
                        }
                here, string savePath = Server.MapPath( "images\\") ;
                defines the location to upload the image file. this folder should be in your project directory.

                if(FileUpload1. PostedFile.Cont entLength != 0)
                {
                ...
                }

                checks the image file is selected or not.
                if the FileUpload1 has a file, then

                FileUpload1.Sav eAs(savePath + FileUpload1.Fil eName);

                saves the file in the location.

                Nothing else...

                Now run the default.aspx file.

                NOTE: This is simple one. it can be more complected.

                a Microsoft's sample link is given here. File Upload

                Hope it's help you. :)

                Comment

                • thevinh92
                  New Member
                  • Aug 2012
                  • 10

                  #9
                  thanks, ariful alam!
                  But, as i said in the first post, "without using the web browser I want to use something in System.Net such as HttpWebRequest" .
                  yes, I want to create a destop application to upload picture to a local asp.net web page. And i think i will use HttpWebRequest, but i don't know how to handle in the server (the asp.net page)!

                  Comment

                  • ariful alam
                    New Member
                    • Jan 2011
                    • 185

                    #10
                    If you have your local ASP.net web site, then why you need a desktop image/file up-loader? Local ASP.net web site means i think you have your site at your own computer. Isn't it?

                    Comment

                    • thevinh92
                      New Member
                      • Aug 2012
                      • 10

                      #11
                      Thanks for your reply!
                      Yes, I know it. But it is my homework, I have to do it.

                      Comment

                      • ariful alam
                        New Member
                        • Jan 2011
                        • 185

                        #12
                        Ok, you can do it by creating FTP accessible application , if your Web Server and image uploader machine not same. you have to create a FTP access desktop software that can upload the image(s) to the Web Server Machine in a specific folder.

                        Or, if your Web Server and image uploader machine are same, than just create a desktop application that can copy-paste your image(s) from the selected location to your local web site's destination location.

                        Hope it's help you. :)

                        Comment

                        • thevinh92
                          New Member
                          • Aug 2012
                          • 10

                          #13
                          thanks for the all you help!
                          But, my teacher made me use HttpWebRequest and http protocol!
                          My idea is: i wil use HttpWebRequest to send Images to the server, and in the server (asp.net page) i will show the Images that i upload! But i don't know how to do!
                          plz, help me!

                          Comment

                          • andrewpattinson
                            New Member
                            • Aug 2012
                            • 6

                            #14
                            1.Start Microsoft Visual Studio .NET
                            2. On the File menu, point to New, and then click Project.
                            3. In the New Project dialog box, click Visual C# Projects under Project Types, and then click ASP.NET Web Application under Templates.
                            4.In the Location box, type the URL to create the project. For more read it <link removed per forum policy>
                            Last edited by Rabbit; Nov 8 '12, 04:22 PM. Reason: Link removed per forum policy.

                            Comment

                            • Frinavale
                              Recognized Expert Expert
                              • Oct 2006
                              • 9749

                              #15
                              Create a desktop application type (Not an ASP.NET web application).

                              Check out the HttpWebRequest class documentation for information on how to create a connection to the web server.

                              Once you have made that connection you can use the GetRequestStrea m method...which returns a Stream object to use to send data.

                              I've never used the HttpWebRequest to send data to a server, so I would stick to the MSDN documentation to learn how to do this. It is most likely that you will have to implement an aspx page that manages the uploading of the images...and that the HttpWebRequest class will connect to this page to send the image to it.

                              So, along with your desktop application, you will be developing the aspx page that manage the upload process and you will have to write an aspx page that displays the images.

                              -Frinny

                              Comment

                              Working...