
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace FileUpload
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnFileUpload_Click(object sender, EventArgs e)
{
UploadPicFile(ImgFileUpload);
}
internal readonly string AllowExt = "jpe|jpeg|jpg|png|tif|tiff|bmp|gif|wbmp|swf|psd";
/// /// 检测扩展名的有效性 ///
/// 文件名扩展名
///
bool CheckValidExt(string sExt)
{
bool flag = false;
string[] aExt = AllowExt.Split('|');
foreach (string filetype in aExt)
{
if (filetype.ToLower() == sExt.Replace(".", ""))
{
flag = true;
break;
}
}
return flag;
}
private void UploadPicFile(System.Web.UI.WebControls.FileUpload Fupload)
{
//文件上传函数
try
{
if (Fupload.HasFile)
{
//判断文件格式
string sExt = Fupload.FileName.Substring(Fupload.FileName.LastIndexOf(".")).ToLower();
if (!CheckValidExt(sExt))
{
lblMsg.Text = "(原图片文件格式不正确!支持的格式有[ " + AllowExt + " ])";
return;
}
//判断文件大小
int intFileLength = Fupload.PostedFile.ContentLength;
if (intFileLength > 1000 * 1000)
{
this.lblMsg.Text = "文件大于1M,不能上传!";
return;
}
Random ran = new Random();
