ASP.NET 中的上传控件 使用
来源:互联网 发布:软件著作权源代码模板 编辑:程序博客网 时间:2024/05/16 19:49
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;
/// <summary>
/// MyUpload 的摘要说明
/// </summary>
public class MyUpload
{
private System.Web.HttpPostedFile postedFile = null;
private string savePath = "";
private string[] extension;
private int fileLength = 0;
public string[] filestype ={ "txt", "doc", "gif" };
public MyUpload()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
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();//开始上传,并显示上传结果";
helpstring += "<font size=3 color=red>";
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 = HttpContext.Current.Server.MapPath("../upimages/") + value;/////////////////////注意这里选取上传文件路径,可以修改成传递参数类型的
}
}
public int FileLength
{
get
{
if (fileLength != 0) return fileLength;
return 1024;
}
set
{
fileLength = value * 1024;
}
}
public string[] Extension
{
get
{
if (extension != null) return extension;
return filestype;
}
set
{
extension = value;
}
}
public string PathToName(string path)
{
int pos = path.LastIndexOf("//");
return path.Substring(pos + 1);
}
public string Upload()
{
int biaoshi = 0;
if (PostedFile.FileName != "")
{
try
{
string fileName = PathToName(PostedFile.FileName);
for (int i = 0; i < Extension.Length; i++)
{
if (fileName.EndsWith(Extension[i]))
{
biaoshi = 1;
}
}
if (biaoshi != 1)
{
string errs = "";
for (int i = 0; i < Extension.Length; i++)
{
errs = errs + Extension[i] + "/";
}
return "您必须选择" + errs + "类型的文件!";
}
if (PostedFile.ContentLength > FileLength) return "文件太大,超过了限定值!";
PostedFile.SaveAs(SavePath + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + fileName.Substring(fileName.Length - 4, 4));
return DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + fileName.Substring(fileName.Length - 4, 4);
}
catch (System.Exception exc) { return exc.Message; }
}
return "请选择文件后再进行上传!";
}
}
////////////////////使用例子,直接复制没有修改/////////////////////
string[] filestype ={ "gif", "jpg", "png" };
MyUpload myload = new MyUpload();
myload.PostedFile = File1.PostedFile;
myload.SavePath = "";
myload.FileLength = 1000;
myload.Extension = filestype;
Label1.Text = myload.Upload();
if (Label1.Text.StartsWith("200"))
{
Label2.Text = "upimages/" + Label1.Text;
Label1.Text = "上传更新成功!";
}
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;
/// <summary>
/// MyUpload 的摘要说明
/// </summary>
public class MyUpload
{
private System.Web.HttpPostedFile postedFile = null;
private string savePath = "";
private string[] extension;
private int fileLength = 0;
public string[] filestype ={ "txt", "doc", "gif" };
public MyUpload()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
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();//开始上传,并显示上传结果";
helpstring += "<font size=3 color=red>";
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 = HttpContext.Current.Server.MapPath("../upimages/") + value;/////////////////////注意这里选取上传文件路径,可以修改成传递参数类型的
}
}
public int FileLength
{
get
{
if (fileLength != 0) return fileLength;
return 1024;
}
set
{
fileLength = value * 1024;
}
}
public string[] Extension
{
get
{
if (extension != null) return extension;
return filestype;
}
set
{
extension = value;
}
}
public string PathToName(string path)
{
int pos = path.LastIndexOf("//");
return path.Substring(pos + 1);
}
public string Upload()
{
int biaoshi = 0;
if (PostedFile.FileName != "")
{
try
{
string fileName = PathToName(PostedFile.FileName);
for (int i = 0; i < Extension.Length; i++)
{
if (fileName.EndsWith(Extension[i]))
{
biaoshi = 1;
}
}
if (biaoshi != 1)
{
string errs = "";
for (int i = 0; i < Extension.Length; i++)
{
errs = errs + Extension[i] + "/";
}
return "您必须选择" + errs + "类型的文件!";
}
if (PostedFile.ContentLength > FileLength) return "文件太大,超过了限定值!";
PostedFile.SaveAs(SavePath + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + fileName.Substring(fileName.Length - 4, 4));
return DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + fileName.Substring(fileName.Length - 4, 4);
}
catch (System.Exception exc) { return exc.Message; }
}
return "请选择文件后再进行上传!";
}
}
////////////////////使用例子,直接复制没有修改/////////////////////
string[] filestype ={ "gif", "jpg", "png" };
MyUpload myload = new MyUpload();
myload.PostedFile = File1.PostedFile;
myload.SavePath = "";
myload.FileLength = 1000;
myload.Extension = filestype;
Label1.Text = myload.Upload();
if (Label1.Text.StartsWith("200"))
{
Label2.Text = "upimages/" + Label1.Text;
Label1.Text = "上传更新成功!";
}
- ASP.NET 中的上传控件 使用
- asp.net 2.0 上传控件的使用
- asp.net 2.0上传控件的使用
- asp.net 2.0 上传控件的使用
- asp.net 上传控件的使用
- asp.net 上传控件
- 不使用ASP.NET中的服务器控件将如何上传文件?
- 使用ASP.NET 2.0中的GridView控件
- 使用ASP.NET 2.0中的GridView控件
- 使用ASP.NET 2.0中的GridView控件
- 使用ASP.NET 2.0中的GridView控件
- 使用ASP.NET 2.0中的ReportViewer控件
- 使用ASP.NET 2.0中的ReportViewer控件
- 使用ASP.NET 2.0中的ReportViewer控件
- 使用ASP.NET 2.0中的ReportViewer控件
- ASP.NET中的login控件如何使用
- 使用ASP.NET 2.0中的GridView控件
- 使用ASP.NET中的Login控件
- web.config文件的配置
- 虛函数,抽象方法,接口之间的区别
- 第6章 图像文件格式
- 第7章 彩色数字电视基础
- 第8章 MPEG简介
- ASP.NET 中的上传控件 使用
- 第9章 MPEG声音
- 第10章 MPEG电视
- 第11章 只读光盘存储器
- Linux那些事儿 之 戏说USB(9)我是谁
- 第12章 光盘存储器的格式
- 第13章 错误检测和校正
- 设计模式学习笔记(八)——Bridge桥接模式
- 动画(FLASH)下载任我行-----保存自己喜欢的动画的方法总结