• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

上传文件

发布: 2007-6-30 18:56 | 作者: admin | 来源: | 查看: 13次 | 进入软件测试论坛讨论

领测软件测试网 /*
Descript:上传文件
Author:Blue.Dream
Date:2004-09-21 22:21
*/
namespace BDStudio.Common
{
using System;
using System.IO;
using System.Web;
/// <summary>
/// 上传单个文件
/// </summary>
public class UpLoadFile
{
private string[] AllowFileType; //所允许的文件类型
private double FileLength; //所允许的文件大小(KB)
private string SavePath; //文件的存储路径
private string SaveFile; //上传后的文件名
private string Error; //存储错误信息
private string FileExtesion; //上传文件的扩展名

/// <summary>
/// 构造函数
/// </summary>
/// <param name="allFileType">允许的文件类型,多个以","分开</param>
/// <param name="fileLength">文件大小</param>
/// <param name="savePath">保存路径</param>
public UpLoadFile(string allFileType,double fileLength,string savePath)
{
char[] sp = {‘’,‘’};
AllowFileType = allFileType.Split(sp);
FileLength = fileLength;
SavePath = savePath;
}

/// <summary>
/// 返回生成的文件名
/// </summary>
public string FileName
{
get
{
return SaveFile;
}
}

/// <summary>
/// 返回出错信息
/// </summary>
public string ErrorMessage
{
get
{
return Error;
}
}

/// <summary>
/// 根据SavePath,生成文件名
/// </summary>
/// <returns></returns>
private string MakeFileName(string fileType)
{
string file = this.SavePath + "\\" + System.DateTime.Now.Year.ToString()+System.DateTime.Now.Month.ToString()+System.DateTime.Now.Day.ToString()+System.DateTime.Now.Hour.ToString()+System.DateTime.Now.Minute.ToString()+System.DateTime.Now.Second.ToString()+this.FileExtesion;
for(; File.Exists(file);)
{
file = this.SavePath + "\\" + System.DateTime.Now.Year.ToString()+System.DateTime.Now.Month.ToString()+System.DateTime.Now.Day.ToString()+System.DateTime.Now.Hour.ToString()+System.DateTime.Now.Minute.ToString()+System.DateTime.Now.Second.ToString()+this.FileExtesion;
}
return file;
}

/// <summary>
/// 检查文件类型
/// </summary>
/// <param name="fileEx">MIME内容</param>
/// <returns></returns>
private bool CheckFileExt(string fileEx)
{
bool result = false;
for(int i = 0; i < this.AllowFileType.Length; i++)
{
if(fileEx.IndexOf(this.AllowFileType[i].ToLower()) > -1)
{
result = true;
break;
}
}
return result;
}

public bool UpLoad()
{
bool result = true;
System.Web.HttpFileCollection objFiles = System.Web.HttpContext.Current.Request.Files;
System.Web.HttpPostedFile objFile = objFiles[0];
try
{
//查看文件长度
if(objFile.ContentLength > (this.FileLength))
{
this.Error = "文件大小超出范允许的范围!";
return false;
}

string fileName = Path.GetFileName(objFile.FileName);
this.FileExtesion = Path.GetExtension(fileName);

if(!CheckFileExt(this.FileExtesion.ToLower()))
{
this.Error = "文件类型"+this.FileExtesion+"不允许!";
return false;
}
//取得要保存的文件名
string UpFile = this.MakeFileName(this.FileExtesion);
//保存文件
objFile.SaveAs(UpFile);
//返回文件名
this.SaveFile = Path.GetFileName(UpFile);

}
catch(Exception e)
{
result = false;
this.Error = e.Message;
}
return result;
}
}
}


延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网