转载:ASP.NET文件上传类
2010-06-29 16:17
239 查看
整理了下文件上传方法,详细见代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
public class Upload
{
private System.Web.HttpPostedFile postedFile = null;
private string savePath = "";
private string extension = "";
private int fileLength = 0;
private string filename = "";
/// <summary>
/// 上传组件
/// </summary>
public System.Web.HttpPostedFile PostedFile
{
get
{
return postedFile;
}
set
{
postedFile = value;
}
}
/// <summary>
/// 保存路径
/// </summary>
public string SavePath
{
get
{
if (savePath != "") return savePath;
return "c:\\";
}
set
{
savePath = value;
}
}
/// <summary>
/// 文件大小
/// </summary>
public int FileLength
{
get
{
if (fileLength != 0) return fileLength;
return 1024;
}
set
{
fileLength = value * 1024;
}
}
/// <summary>
/// 文件护展名
/// </summary>
public string Extension
{
get
{
if (extension != "")
return extension;
return "txt";
}
set
{
extension = value;
}
}
/// <summary>
/// 文件名
/// </summary>
public string FileName
{
get
{
return filename;
}
set
{
filename = value;
}
}
public string PathToName(string path)
{
int pos = path.LastIndexOf(@"\");
return path.Substring(pos + 1);
}
/// <summary>
/// 上传文件
/// </summary>
/// <returns></returns>
public string UploadStart()
{
bool tf = false;
string returnvalue = "";
if (PostedFile != null)
{
try
{
string fileName = PathToName(PostedFile.FileName);
if (filename != "")
{
fileName = filename;
}
string _fileName = "";
string[] Exten = Extension.Split(',');
if (Exten.Length == 0)
{
returnvalue = "你未设置上传文件类型,系统不允许进行下一步操作!$0";
}
else
{
for (int i = 0; i < Exten.Length; i++)
{
if (fileName.ToLower().EndsWith(Exten[i].ToLower()))
{
if (PostedFile.ContentLength > FileLength)
{
returnvalue = "上传文件限制大小:" + FileLength / 1024 + "kb!$0";
}
string IsFileex = SavePath + @"\" + fileName;
if (!Directory.Exists(SavePath)) { Directory.CreateDirectory(SavePath); }
PostedFile.SaveAs(IsFileex);
_fileName = fileName;
tf = true;
returnvalue = IsFileex + "$1";
}
}
if (tf == false)
returnvalue = "只允许上传" + Extension + " 文件!$0";
}
}
catch (System.Exception exc)
{
returnvalue = exc.Message;
}
}
else
{
returnvalue = "上文件失败!$0";
}
return returnvalue;
}
}
原文:/article/5846669.html
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
public class Upload
{
private System.Web.HttpPostedFile postedFile = null;
private string savePath = "";
private string extension = "";
private int fileLength = 0;
private string filename = "";
/// <summary>
/// 上传组件
/// </summary>
public System.Web.HttpPostedFile PostedFile
{
get
{
return postedFile;
}
set
{
postedFile = value;
}
}
/// <summary>
/// 保存路径
/// </summary>
public string SavePath
{
get
{
if (savePath != "") return savePath;
return "c:\\";
}
set
{
savePath = value;
}
}
/// <summary>
/// 文件大小
/// </summary>
public int FileLength
{
get
{
if (fileLength != 0) return fileLength;
return 1024;
}
set
{
fileLength = value * 1024;
}
}
/// <summary>
/// 文件护展名
/// </summary>
public string Extension
{
get
{
if (extension != "")
return extension;
return "txt";
}
set
{
extension = value;
}
}
/// <summary>
/// 文件名
/// </summary>
public string FileName
{
get
{
return filename;
}
set
{
filename = value;
}
}
public string PathToName(string path)
{
int pos = path.LastIndexOf(@"\");
return path.Substring(pos + 1);
}
/// <summary>
/// 上传文件
/// </summary>
/// <returns></returns>
public string UploadStart()
{
bool tf = false;
string returnvalue = "";
if (PostedFile != null)
{
try
{
string fileName = PathToName(PostedFile.FileName);
if (filename != "")
{
fileName = filename;
}
string _fileName = "";
string[] Exten = Extension.Split(',');
if (Exten.Length == 0)
{
returnvalue = "你未设置上传文件类型,系统不允许进行下一步操作!$0";
}
else
{
for (int i = 0; i < Exten.Length; i++)
{
if (fileName.ToLower().EndsWith(Exten[i].ToLower()))
{
if (PostedFile.ContentLength > FileLength)
{
returnvalue = "上传文件限制大小:" + FileLength / 1024 + "kb!$0";
}
string IsFileex = SavePath + @"\" + fileName;
if (!Directory.Exists(SavePath)) { Directory.CreateDirectory(SavePath); }
PostedFile.SaveAs(IsFileex);
_fileName = fileName;
tf = true;
returnvalue = IsFileex + "$1";
}
}
if (tf == false)
returnvalue = "只允许上传" + Extension + " 文件!$0";
}
}
catch (System.Exception exc)
{
returnvalue = exc.Message;
}
}
else
{
returnvalue = "上文件失败!$0";
}
return returnvalue;
}
}
原文:/article/5846669.html
相关文章推荐
- ASP.NET利用HttpHandler实现多扩展名文件下载 转载
- ASP.NET中文件上传下载方法集合[转载与aspcool]
- 收藏的asp.net文件上传类源码
- asp.net利用RAR实现文件压缩解压缩[转载]
- asp.net文件上传下载(转载)
- ASP.NET实现的简单易用文件上传类
- 【转载】Asp.NET大文件上传组件开发总结(二)---提取文件内容
- 【转载】把aspx文件编译成DLL文件-.NET教程,Asp.Net开发
- 认识ASP.NET配置文件Web.config(转载自天极网)
- 在asp.net中读取XML文件信息的4种方法(转载)
- asp.net上传文件大小(转载)
- HTML控件ID和NAME属性的区别,以及如何在asp.net页面的.CS文件中获得.ASPX页面中HTML控件的值(转载)
- ASP.NET将GridView中的数据导出到Excel文件中(转载)
- 【转载】解决ASP.NET上传文件大小限制
- ASP.NET实现的简单易用文件上传类
- 【转载】Asp.NET大文件上传组件开发总结(三)---处理文件内容
- [转载]Asp.Net微型服务器,只有一个文件,并且才300K大小|建议从事Asp.Net开发的博友们人手一份
- Asp.net文件上传类
- Asp.net直接保存文件到客户端(转载)
- 在asp.net利用jquery.MultiFile实现多文件上传(转载)