本文实例讲述了Asp.Net的FileUpload类实现上传文件的方法。分享给大家供大家参考。
具体功能代码如下:
复制代码 代码如下:using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace CSFramework.BLL
{
/// <summary>
/// 支持上传的文件类型
/// </summary>
public enum UploadFileType
{
ArticleAttachment = 1,
Image = 2,
Video = 3,
All = 4
}
/// <summary>
/// 上传文件管理类
/// </summary>
public class CFileUpload
{
private FileUpload _fileUpload;
private string _savePath;
private string _LastUploadedFile = string.Empty;
private bool _AutoGenFileName = false;
private bool _AutoGenWatermark = false;
public string LastUploadedFile { get { return _LastUploadedFile; } }
private string _Error = "";
private string PICTURE_FILE = "[.gif.png.jpeg.jpg]";
private string ZIP_FILE = "[.zip.rar]";
private string MUILT_MEDIA_FILE = "[.mpeg.mpg.fla.wma]";
private int IMG_MAX_WIDTH = 700;//指定宽度
private int IMG_MAX_HEIGHT = 0;//未指定高度
private int MAX_SIZE_UPLOAD = 1024;//最大支持上传小于1MB的文件。
/// <summary>
/// 构造器
/// </summary>
/// <param name="fileUpload">Asp.net FileUpload对象</param>
/// <param name="savePath">保存目录,不包含文件名</param>
/// <param name="autoGenFileName">自动生成文件名</param>
public CFileUpload(FileUpload fileUpload, string savePath, bool autoGenFileName, bool autoGenWatermark)
{
_savePath = savePath;
_fileUpload = fileUpload;
_AutoGenFileName = autoGenFileName;
_AutoGenWatermark = autoGenWatermark;
}
/// <summary>
/// 构造器
/// </summary>
/// <param name="fileUpload">Asp.net FileUpload对象</param>
/// <param name="savePath">保存目录,不包含文件名</param>
public CFileUpload(FileUpload fileUpload, string savePath)
{
_savePath = savePath;
_fileUpload = fileUpload;
}
/// <summary>
/// 上传RAR文件
/// </summary>
public bool UploadRARFile()
{
return DoUpload(ZIP_FILE);
}
/// <summary>
/// 上传视频文件
/// </summary>
public bool UploadVideo()
{
return DoUpload(MUILT_MEDIA_FILE);
}
/// <summary>
/// 上传图片文件
/// </summary>
public bool UploadImage()
{
return DoUpload(PICTURE_FILE);
}
public bool UploadImage(int maxWidth, int maxHeight)
{
this.IMG_MAX_WIDTH = maxWidth;
this.IMG_MAX_HEIGHT = maxHeight;
return DoUpload(PICTURE_FILE);
}
/// <summary>
/// 上传任何支持的文件
/// </summary>
public bool UploadAnySupported()
{
return DoUpload(PICTURE_FILE ZIP_FILE MUILT_MEDIA_FILE);
}
/// <summary>
/// 生成新的文件名
/// </summary>
private string GetNewFileName(string folder, string fileName)
{
if (_AutoGenFileName) //自动生成32位GUID文件名
{
string ext = System.IO.Path.GetExtension(fileName);
string newfile = Guid.NewGuid().ToString().Replace("-", "") ext;
return folder newfile;
}
else
{
if (System.IO.File.Exists(folder fileName))
{
string ext = System.IO.Path.GetExtension(fileName);
string filebody = fileName.Replace(ext, "");
int x = 1;
while (true) //如果文件存在,生成尾部带(x)的文件
{
string newfile = folder filebody "(" x.ToString() ")" ext;
if (!System.IO.File.Exists(newfile))
return folder filebody "(" x.ToString() ")" ext;
else
x ;
}
}
else
return folder fileName;
}
}
/// <summary>
/// 最大支持小于1MB的文件。
/// </summary>
private bool AllowMaxSize(int fileLength)
{
double kb = fileLength / 1024;
return (int)kb < MAX_SIZE_UPLOAD;
}
private bool DoUpload(string allowedExtensions)
{
try
{
bool fileOK = false;
if (!_fileUpload.HasFile) throw new Exception("没有文件!"); //上传控件中如果不包含文件,退出
// 得到文件的后缀
string fileExtension = System.IO.Path.GetExtension(_fileUpload.FileName).ToLower();
// 看包含的文件是否是被允许的文件后缀
fileOK = allowedExtensions.IndexOf(fileExtension) > 0;
if (!fileOK) throw new Exception("不支持的文件格式!");
//检查上传文件大小
fileOK = AllowMaxSize(_fileUpload.FileBytes.Length);
if (!fileOK) throw new Exception("图片文件不能大于" MAX_SIZE_UPLOAD.ToString() "KB!");
try
{
// 文件另存在服务器指定目录下
string savefile = GetNewFileName(_savePath, _fileUpload.FileName);
if (IsUploadImage(fileExtension))//保存图片
{
System.Drawing.Image output = CImageLibrary.FromBytes(_fileUpload.FileBytes);
// 检查图片宽度/高度/大小
if (this.IMG_MAX_WIDTH != 0 && output.Width > this.IMG_MAX_WIDTH)
{
output = CImageLibrary.GetOutputSizeImage(output, this.IMG_MAX_WIDTH);
}
Bitmap bmp = new Bitmap(output);
this.CreateDir(Path.GetDirectoryName(savefile));
bmp.Save(savefile, output.RawFormat);
bmp.Dispose();
output.Dispose();
if (_AutoGenWatermark)
{
WatermarkImage genWatermark = new WatermarkImage();
genWatermark.DrawWords(savefile, AppConfig.Current.WatermarkMain,
AppConfig.Current.WatermarkDesc, float.Parse("0.2"));
}
}
else//其它任何文件
{
this.CreateDir(Path.GetDirectoryName(savefile));
_fileUpload.PostedFile.SaveAs(savefile);
}
_LastUploadedFile = savefile;
return true;
}
catch (Exception ex)
{
throw new Exception("上传文件时发生未知错误!" ex.Message);
}
}
catch (Exception ex)
{
_Error = ex.Message;
return false;
}
}
private void CreateDir(string dir)
{
if (Directory.Exists(dir) == false)
Directory.CreateDirectory(dir);
}
private bool IsUploadImage(string fileExtension)
{
bool isImage = PICTURE_FILE.IndexOf(fileExtension) > 0;
return isImage;
}
}
}
希望本文所述对大家的asp.net程序设计有所帮助。
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]