当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 收藏的asp.net文件上传类源码

ASP.NET
ASP.Net分页控件源码
Community Server专题一:概述Community Server
Community Server专题三:HttpModule
relaxlife.net发布一个自己开发的中文分词程序
RLCSS分词系统更新下载
asp.net获取SQL所有数据库名、所有表名、所有字段名
asp.net下获取Excel所有的工作表名称
Asp.Net常用函数
asp.net下用url重写URLReWriter实现任意二级域名的方法
在.NET中利用XMLHTTP下载文件的代码
在ASP.NET 中实现单点登录
c# .net 生成图片验证码的代码
asp.net中MD5 16位和32位加密函数
自己常用到的自定义公共类(已测试通过)
ASP.NET 2.0下随机读取Access记录的实现方法
.NET(C#)连接各类数据库代码-集锦
nunit使用指南之—NUnit Quick Start
ASP.NET中读取XML文件信息的4种方法与示例代码
asp.net中获取远程网页的内容之一(downmoon原创)
asp.net下获取远程网页的内容之二(downmoon原创)

ASP.NET 中的 收藏的asp.net文件上传类源码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 44 ::
收藏到网摘: n/a

namespace Wmj
{
public class MyUpload
{
private System.Web.HttpPostedFile postedFile=null;
private string savePath="";
private string extension="";
private int fileLength=0;
//显示该组件使用的参数信息
public string Help
{
get{
string helpstring;
helpstring="<font size=3>MyUpload myUpload=new MyUpload(); //构造函数";
helpstring+="myUpload.PostedFile=file1.PostedFile;//设置要上传的文件";
helpstring+="myUpload.SavePath=\"e:\\\";//设置要上传到服务器的路径,默认c:\\";
helpstring+="myUpload.FileLength=100; //设置上传文件的最大长度,单位k,默认1k";
helpstring+="myUpload.Extension=\"doc\";设置上传文件的扩展名,默认txt";
helpstring+="label1.Text=myUpload.Upload();//开始上传,并显示上传结果</font>";
helpstring+="<font size=3 color=red>Design By WengMingJun 2001-12-12 All Right Reserved!</font>";
return helpstring;
}
}
public System.Web.HttpPostedFile PostedFile
{
get
{
return postedFile;
}
set
{
postedFile=value;
}
}
public string SavePath
{
get
{
if(savePath!="") return savePath;
return "c:\\";
}
set
{
savePath=value;
}
}
public int FileLength
{
get
{
if(fileLength!=0) return fileLength;
return 1024;
}
set
{
fileLength=value*1024;
}
}
public string Extension
{
get
{
if(extension!="") return extension;
return "txt";
}
set
{
extension=value;
}
}
public string PathToName(string path)
{
int pos=path.LastIndexOf("\\");
return path.Substring(pos+1);
}
public string Upload()
{
if(PostedFile!=null)
{
try{
string fileName=PathToName(PostedFile.FileName);
if(!fileName.EndsWith(Extension)) return "You must select "+Extension+" file!";
if(PostedFile.ContentLength>FileLength) return "File too big!";
PostedFile.SaveAs(SavePath+fileName);
return "Upload File Successfully!";
}
catch(System.Exception exc)
{return exc.Message;}
}
return "Please select a file to upload!";
}
}
}
用csc /target:Library Wmj.cs 编译成dll供以后多次调用
调用举例
<%@page language="C#" runat="server"%>
<%@import namespace="Wmj"%>
<script language="C#" runat="server">
void Upload(object sender,EventArgs e)
{
MyUpload myUpload=new MyUpload();
// label1.Text=myUpload.Help;
myUpload.PostedFile=file1.PostedFile;
myUpload.SavePath="e:\\";
myUpload.FileLength=100;
label1.Text=myUpload.Upload();
}
</script>
<form enctype="multipart/form-data" runat="server">
<input type="file" id="file1" runat="server"/>
<asp:Button id="button1" Text="Upload" OnClick="Upload" runat="server"/>
<asp:Label id="label1" runat="server"/>
</form>