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

所属分类: 网络编程 / ASP.NET 阅读数: 1710
收藏 0 赞 0 分享
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> 
更多精彩内容其他人还在看

解析WPF实现音频文件循环顺序播放的解决方法

本篇文章是对WPF实现音频文件循环顺序播放的方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

解决.net framework 4.0环境下遇到版本不同编译不通过的方法详解

本篇文章是对.net framework 4.0环境下遇到版本不同编译不通过的解决方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

将文件上传、下载(以二进制流保存到数据库)实现代码

将文件以二进制流的格式写入数据库:首先获得文件路径,然后将文件以二进制读出保存在一个二进制数组中具体请祥看本文,希望对你有所帮助
收藏 0 赞 0 分享

点击提交按钮后DropDownList的值变为默认值实现分析

在点击提交按钮后,页面上所有的绑定到数据库的控件值都恢复到默认值,下面与大家分享下DropDownList的值变为默认值
收藏 0 赞 0 分享

ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法

ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

Linkbutton控件在项目中的简单应用

Button控件可分为button控件、LinkButton控件、ImageButton控件三类,而LinkButton控件则在页面上显示为一个超级链接,下面与大家分享下其具体应用
收藏 0 赞 0 分享

Web.config 和 App.config 的区别分析

Web.config 和 App.config 的区别分析,需要的朋友可以参考一下
收藏 0 赞 0 分享

基于.Net中的数字与日期格式化规则助记词的使用详解

本篇文章是对.Net中的数字与日期格式化规则助记词的使用进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

解决在Web.config或App.config中添加自定义配置的方法详解

本篇文章是对在Web.config或App.config中添加自定义配置的方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

深入本机影像生成器(Ngen.exe)工具使用方法详解

本篇文章是对本机影像生成器(Ngen.exe)工具使用方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享
查看更多