ASP.NET文件上传类
2010-06-17 20:21
225 查看
整理了下文件上传方法,详细见代码:
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;
}
}
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;
}
}
相关文章推荐
- ASP.NET实现的简单易用文件上传类
- 收藏的asp.net文件上传类源码
- ASP.NET 文件上传类 简单好用
- ASP.NET 文件上传类 简单好用
- 简单的asp.net文件上传类
- Asp.net文件上传类
- 转载:ASP.NET文件上传类
- ASP.NET实现的简单易用文件上传类
- 收藏的asp.net文件上传类源码
- 很不错的asp.net文件上传类c# 搜索文件 移动文件 删除文件等
- asp.net 文件上传类
- 支持续传功能的ASP.NET WEB API文件下载服务
- asp.net mvc Post上传文件大小限制
- [ASP.NET]下载txt格式的文件
- [dotnetCore2.0]学习笔记之二: ASP.NET Core中,如何灵活使用静态文件和加载自定义配置
- asp.net&nbsp;请问我如何删除文件?
- ASP.NET上传判断类型,防止黑客上传非法文件
- 在 ASP.NET 2.0 中上载文件(多个文件同时上传)--青蛙非王子(lostfrog)
- asp.net两种删除服务器端文件的方法
- IIS6+asp.net 2.0+HTTP 错误 404 - 文件或目录未找到:您正在搜索的页面可能已经删除、更名或暂时不可用。