I always thought that only the file box can be used to upload files only after runat = "server" is used.
For example:
<Form ID = "form1" method = "Post" runat = "server">
<Input id = "file1" type = "file" name = "file1"/>
<Input id = "submit1" type = "Submit" value = "Submit"/>
</Form>
In fact, this is just a misunderstanding. In. net, the file box can also be used to upload files without using runat = "server. For example:
<Form ID = "form1" method = "Post" enctype = "multipart/form-Data" Action = "D. aspx">
<Input id = "file1" type = "file" name = "file1"/>
<Input id = "submit1" type = "Submit" value = "Submit"/>
</Form>
The difference can be seen from the code. If the file box does not contain runat = "server", you must add
Enctype = "multipart/form-Data" to upload files to the server.
Runat = "server" is used in the first form ". The second one does not. In fact, the server is used and the server is not used.
Runat = "server" is different. After using the form of runat = "server", action must be directed to its own webpage. No
A form with runat = "server" can point to a webpage. In this way, the upload of different files is similar to that of ASP.
But the code is much easier.
The processing code for uploading different files is as follows:
D. aspx. CS
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Public partial class D: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (request. Files. Count> 0)
{
Httppostedfile F = request. Files [0];
F. saveas (server. mappath ("002.jpg "));
}
}
}
Conclusion: request. files can be uploaded in two ways. In addition, there are no file upload methods on the code page.
The difference lies in the designer. Close (runat = server) method, the file box must be added with runat = server, form also
Runat = server must be added. Open (submit form), the file box and form do not add runat = server, that is, they are written as pure HTML
Code !!! (So add method = post enctype = "multipart/form-Data" in form ").
Multi-upload: The interface must have multiple selection file text boxes.
Httpfilecollection filecol = request. files;
String tempname, filename, ext; int COUNT = 0 ;;
For (INT I = 0; I <filecol. Count; I ++)
{
Try
{
Httppostedfile file = filecol [I];
Tempname = file. filename; Ext = system. Io. Path. getextension (tempname );
If (! Checkfileextension (EXT) // use the method defined below to check whether the uploaded file suffix is allowed to be uploaded
Continue;
Filename = datetime. Now. tostring ("yyyymmddhhmmssfff") + ext; file. saveas (server. mappath ("upload/") + filename );
System. Threading. thread. Sleep (1); // prevents duplicate file names
Count ++;
} Catch
{
Continue;
}
}