Use ASP. NET to upload files to the server File Upload is a very practical technology and widely used in ASP. net itself in the previous version of ASP to achieve this function, you must use third-party components or develop their own components, now, with ASP. net is much simpler to implement. We can implement the upload function without using any components. For convenience, we can divide file upload into two types: single file upload and multi-file upload.
Upload a single file Let's first introduce the method of uploading a single file. It's easy to upload a single file, The complete code for uploading a single file is as follows: <% @ Import namespace = "system" %> <% @ Import namespace = "system. Web. httppostedfile" %> <% @ Import namespace = "system. Web. UI. htmlcontrols. htmlinputfile" %> <Script language = "VB" runat = "server"> Sub upload (SRC as object, e as eventargs) If uploadfile. postedfile. contentlength = 0 then Showuploadfile. innertext = "Upload Failed or the file does not exist! " Else 'Get file name Dim temp () as string = Split (uploadfile. postedfile. filename ,"/") Dim filename as string = temp (temp. Length-1) 'Save the file Uploadfile. postedfile. saveas (server. mappath (".") & "/files/" & filename) 'Display upload result Showuploadfile. innerhtml = "the file is uploaded successfully! <Br> Upload File Name: "& filename End if End sub </SCRIPT> <HTML> <Body> <Form runat = "server" enctype = "multipart/form-Data"> <Input type = "file" id = "uploadfile" runat = "server" size = "50"> <br> <Asp: button runat = "server" text = "Upload now" onclick = "Upload"/> </Form> <HR> <br> <Span id = "showuploadfile" runat = "server"> </span> </Body> </Html> Save the above Code as a. aspx file, and then create a new directory file to store the file in the directory where the file is located. Run the file. First feel the effect, and then continue to see the following explanation. Use ASP.. NET is required to upload files.. NET Framework: httppostedfile and htmlinputfile. The namespace of these two classes is system. web. httppostedfile and system. web. UI. htmlcontrols. htmlinputfile, so we need to pilot the two namespaces at the beginning of the file, The postedfile indicates the file uploaded to the server. It contains several common attributes: Contentlength: file size; Filename: Detailed path and file name of the uploaded file; Contenttype: the type of the file to be uploaded. The split function is used to obtain the file name, because the detailed path and file name are obtained through postedfile. filename.
Multi-File Upload The so-called multi-File Upload means to upload multiple files at the same time. This single file upload is mostly the same, the difference is that multi-File Upload uploads all files to the server as a collection of files. What we need is to split the file set into a single file, the remaining processing method is the same as uploading a single file. First, you need to know the maximum number of files to be uploaded at the same time, and then you need to put the following htmlinput controls between forms: <Input type = "file" runat = "server" size = "50"> Note: The htmlinput control does not need to set the ID. So how can I retrieve files from the file set uploaded to the server? See the following code: Dim I as integer For I = 0 to request. Files. Count-1 'Use request. Files () to obtain uploaded files one by one Dim myfile as httppostedfile = request. Files (I) 'Here myfile is equivalent to postedfile in the previous example. You can use myfile. filename to get the file name, etc 'The processing code here is the same as uploading a single file. Next Now that you have mastered the ASP. NET File Upload technology, it is not difficult to make a good ASP. NET upload program by using flexible applications, beautifying the upload interface. |