C # File upload to server--reprint to WCJ1984ABC

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.