C # uploading files
Webconfig Configuration
<!--file Upload type-
<add key= "FileType" value= ". Doc,.xls,.txt,.rar"/>
<add key= "Picturetye" value= ". Jpg|. Gif|. Png|. bmp|. psd|. Svg| " />
<!--upload file size--
<add key= "Filesizelimit" value= "102400"/>
#region determine the type of upload file
protected bool Isallowablefiletype ()
{
Read from Web. config to determine file type restrictions
String strfiletypelimit = configurationmanager.appsettings["FileType"]. ToString ();
Whether the current file name extension is included in this string
if (Strfiletypelimit.indexof (Path.getextension (fileup.filename). ToLower ())! =-1)
{
return true;
}
Else
return false;
}
protected bool Isallowablepicturetype ()
{
Read from Web. config to determine picture type restrictions
String strfiletypelimit = configurationmanager.appsettings["Picturetye"]. ToString ();
Whether the current file name extension is included in this string
if (Strfiletypelimit.indexof (Path.getextension (fileup.filename). ToLower ())! =-1)
{
return true;
}
Else
return false;
}
#endregion
#region Determine file size limits
private bool Isallowablefilesize ()
{
To read from Web. config to determine file size limits
Double ifilesizelimit = Convert.ToInt32 (configurationmanager.appsettings["Filesizelimit"]) * 1024;
Determine if the file exceeds the limit
if (Ifilesizelimit > FileUp.PostedFile.ContentLength)
{
return true;
}
Else
{
return false;
}
}
#endregion
protected void Btnupfile_click (object sender, EventArgs e)
{
To see if you have uploaded files
if (FileUp.PostedFile.ContentLength > 0)
{
if (Isallowablefiletype ())
{
if (Directory.Exists (Server.MapPath ("~/file")) = = false)//determine if the folder exists and if it does not exist create
{
Directory.CreateDirectory (Server.MapPath ("~/file"));
}
Else
if (Isallowablefilesize ())
{
String uploadfilepath = configurationmanager.appsettings["UploadFile"]. ToString ();
String uploadfilepath = Server.MapPath ("file\\");
string fullName = FileUp.PostedFile.FileName;
String newName = DateTime.Now.Ticks.ToString () + fullname.substring (Fullname.lastindexof ("."));
Fileup.saveas (Uploadfilepath + newName);
Lblfileurl.text = fullname.substring (fullname.lastindexof ("\ \") + "upload succeeded";
Lblsavefilename.text = NewName;
}
Else
Messegebox.show (this, "file too big, upload failed");
}
Else
Messegebox.show (This, "file type is incorrect, upload failed");
}
Else
{
Messegebox.show (This, "Upload file is empty, upload failed");
}
}
Here, the files have been uploaded to a file folder, of course, if you want to save the path to the database, you need to write another way to save the path to the database, here is not to repeat.
C # File upload to server--reprint to WCJ1984ABC