欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

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

时间:2021-09-22 07:10:07|栏目:.NET代码|点击:
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> 

上一篇:C#并发编程入门教程之概述

栏    目:.NET代码

下一篇:Ajax Throws Sys.WebForms.PageRequestManagerErrorException with Response.Redirect的解决方法

本文标题:收藏的asp.net文件上传类源码

本文地址:http://www.codeinn.net/misctech/176656.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有