All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
HTML语言剖析(十四)特殊字符_[Html教程]_0
提高效率,精选ASP.NET实用代码片段_[Asp.Net教程]
C#提交数据到服务器并返回的处理_[Asp.Net教程]
利用ASP.NET实现分页管理器(高级篇)_[Asp.Net教程]
SQL数据库高级教程:学习 SQL JOIN_[SQL Server教程]
在ASP.NET中利用VWD操作数据库简介_.net资料_编程技术
PHP转到另一网页实现办法一,二,三_[PHP教程]
C#教程:C#中的特殊运算符
动态SQL四种类型的语句格式_mssql学习_编程技术
实例详细讲解PHP中使用的运算符号_php资料_编程技术
Reading number is top 10 pictures
中国处女图鉴2
From China fortress sora aoi3
七步解决性冷淡
Wild animals melee moment of life and death1
人美胸美腿更美2
采访谢楠
沙漠里的美女
The goddess of the single reason2
这两天,中国人民到处都可以“看海”了
到南昌西站了1
Download software ranking
超级战舰
Unix video tutorial15
尖东毒玫瑰B
Detective task-the top secret prostitution files
ASP.NET.2.0.XML.高级编程(第3版)
Ashlynn Video4
linux安装大全
Tram sex maniac 2 (H) rar bag14
VC++6.0简体中文版
jdk1.5
delv published in(发表于) 2014/1/27 6:48:51 Edit(编辑)
ASP.NET程序上传文件功能的具体实例代码_[Asp.Net教程]

ASP.NET程序上传文件功能的具体实例代码_[Asp.Net教程]

ASP.NET程序上传文件功能的具体实例代码_[Asp.Net教程]

//asp.net 中上传文件的源代码,测试成功!


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;


///


/// Summary description for UploadFile
///

public class UploadFile
{
private string[] AllowFileType; //所允许的文件类型
private double FileLength; //所允许的文件大小(KB)
private string SavePath; //文件的存储路径
private string SaveFile; //上传后的文件名
private string Error; //存储错误信息
private string FileExtesion; //上传文件的扩展名


///


/// 构造函数
///

/// 允许的文件类型,多个以","分开
/// 文件大小
/// 保存路径
public UploadFile(string allFileType, double fileLength, string savePath)
{
char[] sp = { ',' };
AllowFileType = allFileType.Split(sp);
FileLength = fileLength * 1024;
SavePath = savePath;
}


///


/// 返回生成的文件名
///

public string FileName
{
get
{
return SaveFile;
}
}


///


/// 返回出错信息
///

public string ErrorMessage
{
get
{
return Error;
}
}


///


/// 根据SavePath,生成文件名
///

///
private string MakeFileName(string fileType,string fileName)
{
string file = this.SavePath + "\\" + DateTime.Now.ToString("yyMMddhhmmss") + fileName;
while (File.Exists(file))
{
file = this.SavePath + "\\" + DateTime.Now.ToString("yyMMddhhmmss") + fileName;
}
return file;
}


///


/// 检查文件类型
///

/// MIME内容
///
private bool CheckFileExt(string fileEx)
{
bool result = false;
for (int i = 0; i < this.AllowFileType.Length; i++)
{
if (fileEx.IndexOf(this.AllowFileType[i].ToLower()) > -1)
{
result = true;
break;
}
}
return result;
}


public bool UpLoad(System.Web.UI.WebControls.FileUpload file)
{
bool result = true;
try
{
//查看文件长度
if (file.PostedFile.ContentLength > (this.FileLength))
{
this.Error = "文件大小超出范允许的范围!";
return false;
}


string fileName = Path.GetFileName(file.PostedFile.FileName);
this.FileExtesion = Path.GetExtension(fileName);


if (!CheckFileExt(this.FileExtesion.ToLower()))
{
this.Error = "文件类型" + this.FileExtesion + "不允许!";
return false;
}
//取得要保存的文件名
string UpFile = this.MakeFileName(this.FileExtesion, fileName);
//保存文件
file.PostedFile.SaveAs(UpFile);


//返回文件名
this.SaveFile = Path.GetFileName(UpFile);


}
catch (Exception e)
{
result = false;
this.Error = e.Message;
}
return result;
}


}



本文由设计家园 收集整理






添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.