当前位置: 编程技术>.net/c#/asp.net
asp.net简单的文件上传代码
来源: 互联网 发布时间:2014-08-30
本文导语: 代码很简单,仅作为学习使用。 代码如下: const string _RegSubFix = @"/.[/w]+$"; const string _RegZip = @"/.zip|/.rar"; protected void btnUpload_Click(object sender, EventArgs e) { if (filUpload.HasFile) { string subfix = Regex.Match(filUpload.FileName, _RegSubFix).Value.T...
代码很简单,仅作为学习使用。
代码如下:
const string _RegSubFix = @"/.[/w]+$";
const string _RegZip = @"/.zip|/.rar";
protected void btnUpload_Click(object sender, EventArgs e)
{
if (filUpload.HasFile)
{
string subfix = Regex.Match(filUpload.FileName, _RegSubFix).Value.ToLower();
if (filUpload.PostedFile.ContentType == "application/octet-stream" && !Regex.IsMatch(filUpload.FileName, _RegZip))
{
WebManageShell.WebHelp.Alert(this.Page, "不受支持的文件格式。");
return;
}
string folder = ConfigurationManager.AppSettings["UploadPath"] + "//" + DateTime.Now.ToString("yyyyMM");
try
{
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
string file = folder + "//" + DateTime.Now.ToFileTimeUtc().ToString() + subfix;
filUpload.SaveAs(file);
WebManageShell.WebHelp.Alert(this.Page,"文件上传成功。");
}
catch
{
WebManageShell.WebHelp.Alert(this.Page, "文件上传失败。");
}
}
else
{
WebManageShell.WebHelp.Alert(this.Page, "你没有上传文件。");
}
}
const string _RegZip = @"/.zip|/.rar";
protected void btnUpload_Click(object sender, EventArgs e)
{
if (filUpload.HasFile)
{
string subfix = Regex.Match(filUpload.FileName, _RegSubFix).Value.ToLower();
if (filUpload.PostedFile.ContentType == "application/octet-stream" && !Regex.IsMatch(filUpload.FileName, _RegZip))
{
WebManageShell.WebHelp.Alert(this.Page, "不受支持的文件格式。");
return;
}
string folder = ConfigurationManager.AppSettings["UploadPath"] + "//" + DateTime.Now.ToString("yyyyMM");
try
{
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
string file = folder + "//" + DateTime.Now.ToFileTimeUtc().ToString() + subfix;
filUpload.SaveAs(file);
WebManageShell.WebHelp.Alert(this.Page,"文件上传成功。");
}
catch
{
WebManageShell.WebHelp.Alert(this.Page, "文件上传失败。");
}
}
else
{
WebManageShell.WebHelp.Alert(this.Page, "你没有上传文件。");
}
}