http://dotnet.aspx.cc/ShowDetail.aspx?id=D8F961C3-CBC1-4591-143D-236B572EB89F
解决方案 »
- treeview中如何右键点击节点时弹出自定义下拉菜单
- 关于弹出子窗口的问题谢谢各位大虾
- .net与VSS文件编码问题
- 在收藏夹中和在地址栏中打开同一个url地址又什么区别
- 如何实现浮动窗口,并且点击这个窗口上的图标的时候去执行父窗口中的JS函数?
- remoting主要是做什么用的
- 一个关于Session的问题..
- 点击BUTTON按钮,如何弹出确认对话框
- asp.net网站长时间登陆后失效,不能登出和进入其他需要登陆后才能进入的页面。。。
- @html htmlAttributes属于用法
- 一个页面需要许多autopostback但又不想整个页面一直刷,怎么办
- 我工作一年了,这都不懂,还望大家指教!!
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();
}
[Test.Htm]
</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>
{
HttpPostedFile hpf = UploadFile.PostedFile;
//FileName.Text = hpf.FileName;
if(hpf.ContentLength > 5242880)
{
Response.Write("<script language=javascript>");
Response.Write("alert('上传文件太大,系统限制最大为5M!');");
Response.Write("</script>");
return;
} char[] de = {'\\'};
string[] AFilename = hpf.FileName.Split(de);
string strFilename = AFilename[AFilename.Length - 1];
//rname.Text = strFilename;
FileInfo fi = new FileInfo(Path.Combine(FolderPath.Value,strFilename));
if(fi.Exists)
{
Response.Write("<script language=javascript>");
Response.Write("confirm('文件已经存在,是否覆盖?');");
Response.Write("</script>");
return;
}
else
{
hpf.SaveAs(FolderPath.Value+@"\"+strFilename);
Response.Write("<script language=javascript>");
Response.Write("alert('文件已经上传!');");
Response.Write("</script>");
DisplayList(FolderPath.Value);
return;
}
}
catch(Exception ex)
{
ErrorText.Text = ex.Message;
}