First, front page:
<form runat= "Server" > <asp:fileupload id= "FileUpload1" runat= "Server"/>
<asp:button id= "Button1" runat= "Server" onclick= "button1_click" text= "Upload"/>
<br/>
<br/>
<asp:label id= "Label1" runat= "Server" text= "Label" ></asp:Label>
<br/>
<asp:image id= "Image1" runat= "Server" visible= "False"/>
<asp:label id= "FILEURL" runat= "Server" text= "Label" visible= "false" ></asp:Label>
<asp:label id= "FileName" runat= "Server" text= "Label" visible= "false" ></asp:Label>
<asp:label id= "filesize" runat= "Server" text= "Label" visible= "false" ></asp:Label>
<asp:label id= "Label2" runat= "Server" text= "Label" ></asp:Label>
</form>
Second, the background code:
protected void Button1_Click (object sender, EventArgs e)
{
bool FileOK = true;
Upload path to File
String path = Server.MapPath ("files\\");
Determine if the upload folder exists, if it does not exist, create
if (! Directory.Exists (PATH))
{
Create a folder
Directory.CreateDirectory (path);
}
if (fileupload1.hasfile)
{
Types allowed to upload
String[] allowextesions = {". doc", ". xls", ". rar", ". zip", ". ppt"};
for (int i = 0; i < allowextesions.length; i++)
//{
if (fileextesion = = Allowextesions[i])
// {
File format allowed to upload correctly
FileOK = true;
// }
//}
String name = fileupload1.filename;//Gets the name of the uploaded file.
int size = fileupload1.postedfile.contentlength;//file size.
if (Size > (10 * 1024))
{
FileOK = false;
Label2.Text = "file too large";
}
String type = fileupload1.postedfile.contenttype;//file type.
Get the type of upload file (suffix)
String fileextesion = System.IO.Path.GetExtension (fileupload1.filename). ToLower ();
String type2 = name. Substring (name. LastIndexOf (".") + 1);//LastIndexOf () the last index position matches. +1 of the Substring () is overloaded.
if (FileOK)
{
Try
{
Random rannum = new Random ();
A random positive integer between 1 and 1000
int num = Rannum.next (1, 1000);
Get current time
String newname = System.DateTime.Now.ToString ("Yyyymmddhhmmssffff");
Declaring file names to prevent duplication
newname = newname + num + fileextesion;
String ipath = Server.MapPath ("files\\upimg") + "\" + newname; Gets the path to the upimg directory below the root directory.
String fpath = Server.MapPath ("files\\upfile") + "\" + newname;
String wpath = "files\\upimg\\" + newname; Get virtual path
if (fileextesion = = ". jpg" | | fileextesion = = ". gif" | | fileextesion = ". bmp" | | fileextesion = = ". png")
{
Fileupload1.saveas (Ipath); Save method, parameter is an address string.
Image1.imageurl = Wpath;
Label1.Text = "The filename you send is:" + name + "<br> file size is:" + size + "Byte <br> file type is:" + type +
The <br> suffix is: "+ fileextesion +" <br> The actual path is: "+ ipath +" <br> virtual path is: "+ fpath;
Image1.visible = true;
Fileurl.text = Ipath;
}
Else
{
Image1.visible = false;
Fileupload1.saveas (Fpath);
Label1.Text = "The filename you send is:" + name + "<br> file size is:" + size + "Byte <br> file type is:" + type +
The <br> suffix is: "+ fileextesion +" <br> The actual path is: "+ ipath +" <br> virtual path is: "+ fpath;
Fileurl.text = Fpath;
}
FileUpload1.PostedFile.SaveAs (path + newname);
session["filename"] = newname;
Label2.Text = "Upload success";
Filename.text = name;
FileSize. Text = size. ToString ();
Lab_upload. Text = "Upload success";
}
catch (Exception ex)
{
Label2.Text = "Upload Failed";
Throw ex;
}
}
}
Else
{
File not selected
Label2.Text = "No files have been selected, please select files";
Return
}
}