有些时候,我们需要一种危机,来激发我们自身的潜能,唤醒我们内心深处被掩藏已久的人生激情,来实现人生的最大价值。这一秒不放弃,下一秒就有希望!
1、Upfile.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Upfile.aspx.cs" Inherits="Inc_Upfile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml"; >
<head runat="server">
<title>上传文件</title>
<link rel="nofollow noopener noreferrer" href="../Manage/Style.Css" type="text/css" rel=Stylesheet />
</head>
<body>
<form id="form1" runat="server">
<div style="left: 0px; clip: rect(0px auto auto 0px); position: absolute; top: 0px">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传文件" CssClass="btn2" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
</form>
</body>
</html>
Upfile.aspx.cs 代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class Inc_Upfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Cut By 梦溪苑。
}
protected void Button1_Click(object sender, EventArgs e)
{
AllSheng.upload UpFiles = new AllSheng.upload();
//HttpPostedFile File = FileUpload1.PostedFile;
// AllSheng.UploadObj.PhotoSave("/", FileUpload1);
HttpFileCollection files = HttpContext.Current.Request.Files;
UpFiles.Path = "../UpLoadfiles";
String ReStr= UpFiles.SaveAs(files).ToString();
Label1.Text = ReStr;
UpFiles = null;
}
}
3、类文件: 代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/**//// <summary>
/// Cls_Upfile 的摘要说明
/// </summary>
///
namespace AllSheng
{
public class upload
{
变量#region 变量
System.Web.HttpPostedFile postedFile;
protected string localFileName;//原文件名(含扩展名)
protected string localFileExtension;//原扩展名
protected long localFileLength;//原文件大小
protected string localFilePath;//原文件路径
protected string saveFileName;//保存的文件名(含扩展名)
protected string saveFileExtension;//保存的扩展名
//protected long saveFileLength;//保存的文件大小
protected string saveFilePath;//保存文件的服务器端的完整路径
protected string saveFileFolderPath;//保存文件的服务器端的文件夹路径
private string path = null;
private string fileType = null;
private int sizes = 0;
#endregion
upload():初始化变量#region upload():初始化变量
/**//// <summary>
/// 初始化变量
/// </summary>
public upload()
{
path = @"uploadimages"; //上传路径
fileType = "jpg|gif|bmp|jpeg|png|rar|doc";
sizes = 200; //传文件的大小,默认200KB
}
#endregion
设置传入的值:Path/Sizes/FileType#region 设置传入的值:Path/Sizes/FileType
/**//// <summary>
/// 设置上传路径,如:uploadimages
/// </summary>
public string Path
{
set
{
path = @"" + value + @"";
}
}
/**//// <summary>
/// 设置上传文件大小,单位为KB
/// </summary>
public int Sizes
{
set
{
sizes = value;
}
}
/**//// <summary>
/// 设置上传文件的类型,如:jpg|gif|bmp
/// </summary>
public string FileType
{
set
{
fileType = value;
}
}
#endregion
SaveAs()上传文件#region SaveAs()上传文件
public string SaveAs(System.Web.HttpFileCollection files)
{
string myReturn = "";
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
postedFile = files[iFile];
//获得文件的上传的路径
localFilePath = postedFile.FileName;
//判断上传文件路径是否为空
if (localFilePath == null || localFilePath == "")
{
//message("您没有上传数据呀,是不是搞错了呀!");
//break;
continue;
}
else
{
判断文件大小#region 判断文件大小
//获得上传文件的大小
localFileLength = postedFile.ContentLength;
//判断上传文件大小
if (localFileLength >= sizes * 1024)
{
message("上传的图片不能大于" + sizes + "KB");
break;
}
#endregion
文件夹#region 文件夹
//获取保存文件夹路径
saveFileFolderPath = getSaveFileFolderPath(path);
#endregion
文件名#region 文件名
//获得原文件名(含扩展名)
localFileName = System.IO.Path.GetFileName(postedFile.FileName);
saveFileName = DateTime.UtcNow.ToString("yyyy" + "MM" + "dd" + "HH" + "mm" + "ss" + "ffffff");//"yyyy"+"MM"+"dd"+"HH"+"mm"+"ss"+"ffffff"
#endregion
扩展名#region 扩展名
//获取原文件扩展名
localFileExtension = getFileExtension(localFileName);
//如果为真允许上传,为假则不允许上传
if (localFileExtension == "")
{
message("目前本系统支持的格式为:" + fileType);
}
//得到保存文件的扩展名,可根据需要更改扩展名
saveFileExtension = localFileExtension;
#endregion
//得到保存文件的完整路径
saveFilePath = saveFileFolderPath + saveFileName + saveFileExtension;
postedFile.SaveAs(saveFilePath);
myReturn = myReturn + ((myReturn == "" || myReturn == null) ? "" : "|") + path.TrimStart(new char[] { '' }) + saveFileName + saveFileExtension;
//以下对文章的内容进行一些加工
System.Web.HttpContext.Current.Response.Write("<script>parent.Article_Content___Frame.FCK.EditorDocument.body.innerHTML+='<img src=" + saveFileName + saveFileExtension + " "+" border=0 />'</SCRIPT>");
}
}
}
catch
{
//异常
message("出现未知错误!");
myReturn = null;
}
return myReturn;
}
#endregion
getSaveFileFolderPath( ):获得保存的文件夹的物理路径#region getSaveFileFolderPath( ):获得保存的文件夹的物理路径
/**//// <summary>
/// 获得保存的文件夹的物理路径
/// 返回保存的文件夹的物理路径,若为null则表示出错
/// </summary>
/// <param name="format">保存的文件夹路径 或者 格式化方式创建保存文件的文件夹,如按日期"yyyy"+"MM"+"dd":20060511</param>
/// <returns>保存的文件夹的物理路径,若为null则表示出错</returns>
private string getSaveFileFolderPath(string format)
{
string mySaveFolder = null;
try
{
string folderPath = null;
//以当前时间创建文件夹,
//!!!!!!!!!!!!以后用正则表达式替换下面的验证语句!!!!!!!!!!!!!!!!!!!
if (format.IndexOf("yyyy") > -1 || format.IndexOf("MM") > -1 || format.IndexOf("dd") > -1 || format.IndexOf("hh") > -1 || format.IndexOf("mm") > -1 || format.IndexOf("ss") > -1 || format.IndexOf("ff") > -1)
{
//以通用标准时间创建文件夹的名字
folderPath = DateTime.UtcNow.ToString(format);
mySaveFolder = System.Web.HttpContext.Current.Server.MapPath(".") + @"" + folderPath + @"";
}
else
{
mySaveFolder = System.Web.HttpContext.Current.Server.MapPath(".") + format;
}
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(mySaveFolder);
//判断文件夹否存在,不存在则创建
if (!dir.Exists)
{
dir.Create();
}
}
catch
{
message("获取保存路径出错");
}
return mySaveFolder;
}
#endregion
getFileExtension( ):获取原文件的扩展名#region getFileExtension( ):获取原文件的扩展名
/**//// <summary>
/// 获取原文件的扩展名,返回原文件的扩展名(localFileExtension),该函数用到外部变量fileType,即允许的文件扩展名.
/// </summary>
/// <param name="myFileName">原文件名</param>
/// <returns>原文件的扩展名(localFileExtension);若返回为null,表明文件无后缀名;若返回为"",则表明扩展名为非法.</returns>
private string getFileExtension(string myFileName)
{
string myFileExtension = null;
//获得文件扩展名
myFileExtension = System.IO.Path.GetExtension(myFileName);//若为null,表明文件无后缀名;
//分解允许上传文件的格式
if (myFileExtension != "")
{myFileExtension = myFileExtension.ToLower();//转化为小写
}
string[] temp = fileType.Split('|');
//设置上传的文件是否是允许的格式
bool flag = false;
//判断上传的文件是否是允许的格式
foreach (string data in temp)
{
if (("." + data) == myFileExtension)
{
flag = true;
break;
}
}
if (!flag)
{
myFileExtension = "";//不能设置成null,因为null表明文件无后缀名;
}
return myFileExtension;
}
#endregion
message( ):弹出消息框#region message( ):弹出消息框
/**//// <summary>
/// 弹出消息框,显示内容(msg),点击"确定"后页面跳转到该路径(url)
/// </summary>
/// <param name="msg">显示内容</param>
/// <param name="url">跳转路径</param>
private void message(string msg, string url)
{
System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');window.location='" + url + "'</script>");
}
/**//// <summary>
/// 弹出消息框,显示内容(msg),无跳转
/// </summary>
/// <param name="msg">显示内容</param>
private void message(string msg)
{
System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');</script>");
}
#endregion
}
}
到此这篇关于C# 通用文件上传类就介绍到这了。我坚信,人应该有力量,揪着自己的头发把自己从泥地里拔起来。更多相关C# 通用文件上传类内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!