httppostedfile php,ASP.NET MVC HttpPostedFileBase文件上传

[导读]HttpPostedFileBase文件上传,支持多文件一次上传,如有图片,则支持略缩图保存文件传输信息封装

HttpPostedFileBase文件上传,支持多文件一次上传,如有图片,则支持略缩图保存

文件传输信息封装/// 

/// 文件生成方式

/// 

public class UpFileMessage

{

/// 

/// 文件名

/// 

public string OriginalFileName { get; set; }

/// 

/// 是否保存略缩图

/// 

public bool IsImage { get; set; }

/// 

/// 文件流

/// 

public Stream FileStream { get; set; }

/// 

/// 生成缩略图的方式

/// [WH]-指定宽高

/// [H]-指定高,按比例缩放宽

/// [W]-指定宽,按比例缩放高

/// 

public string Mode { get; set; }

/// 

/// 略缩图高度

/// 

public int? ThumbHeight { get; set; }

/// 

/// 略缩图宽度

/// 

public int? ThumbWidth { get; set; }

}

文件上传返回结果/// 

/// 文件上传返回结果

/// 

public class UpFileResultMessage

{

/// 

/// 文件保存是否成功

/// 

public bool IsSuccess { get; set; }

/// 

/// 错误消息

/// 

public string Message { get; set; }

/// 

/// 原始文件名-(无扩展名)

/// 

public string FileName { get; set; }

/// 

/// 文件名扩展名

/// 

public string FileSuffix { get; set; }

/// 

/// 文件名保存路径

/// 

public string FilePath { get; set; }

/// 

/// 文件类型为图片时

/// 缩略图保存路径

/// 

public string ThumbPath { get; set; }

/// 

/// 保存文件名(无扩展名)

/// 

public string SaveFileName { get; set; }

/// 

/// 文件自增ID

/// 

public int[] FileIdArray { get; set; }

}

文件上传类库

需引用System.Web命名空间,并对 [System.Web.UI.Page] 进行继承,调用Server.MapPath方法public class FileUtil : System.Web.UI.Page

{

/// 

/// 图片上传

/// 

/// 文件生成方式

/// 

public UpFileResultMessage UpLoadFile(UpFileMessage fileMessage)

{

try

{

string now = DateTime.Today.ToString("yyyyMMdd");

string guid = Guid.NewGuid().ToString();

//获取文件扩展名

var fileSuffix = Path.GetExtension(fileMessage.OriginalFileName);

var uploadFolder = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(ParmsConfig.UpFilePathUrl), now);

if (!Directory.Exists(uploadFolder))

{

Directory.CreateDirectory(uploadFolder);

}

//保存文件名

string saveFileName = guid + fileSuffix;

string filePath = Path.Combine(uploadFolder, saveFileName);

UpFileResultMessage upFileResult = new UpFileResultMessage()

{

IsSuccess = true,

FileName = Path.GetFileNameWithoutExtension(fileMessage.OriginalFileName),

FileSuffix = fileSuffix,

FilePath = string.Format(@"{0}/{1}", ParmsConfig.UpFileIPAddressUrl, now),

SaveFileName = guid

};

using (Stream sourceStream = fileMessage.FileStream)

{

using (FileStream targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))

{

const int bufferLen = 1024 * 4;//4KB

byte[] buffer = new byte[bufferLen];

int count = 0;

while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)

{

targetStream.Write(buffer, 0, count);

}

}

//上传文件为图片时,需创建缩略图

if (fileMessage.IsImage)

{

var uploadThumbFolder = Path.Combine(uploadFolder, "Thumb");

if (!Directory.Exists(uploadThumbFolder))

{

Directory.CreateDirectory(uploadThumbFolder);

}

using (FileStream targetStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None))

{

System.Drawing.Image image = System.Drawing.Image.FromStream(targetStream);

int width = image.Width;

int height = image.Height;

int thumbWidth = 0;

int thumbHeight = 0;

switch (fileMessage.Mode)

{

case "WH":  //指定高宽缩放(可能变形)

thumbWidth = fileMessage.ThumbWidth.HasValue ? fileMessage.ThumbWidth.Value : 200;

thumbHeight = fileMessage.ThumbHeight.HasValue ? fileMessage.ThumbHeight.Value : 200;

break;

case "W":   //指定宽,高按比例

thumbWidth = fileMessage.ThumbWidth.HasValue ? fileMessage.ThumbWidth.Value : 200;

thumbHeight = height * thumbWidth / width;

break;

case "H":   //指定高,宽按比例

thumbHeight = fileMessage.ThumbHeight.HasValue ? fileMessage.ThumbHeight.Value : 200;

thumbWidth = width * thumbHeight / height;

break;

default:

thumbWidth = fileMessage.ThumbWidth.HasValue ? fileMessage.ThumbWidth.Value : 200;

thumbHeight = height * thumbWidth / width;

break;

}

string thumbFilePath = Path.Combine(uploadThumbFolder, saveFileName);

CreateThumbnail(thumbFilePath, targetStream, thumbWidth, thumbHeight);

upFileResult.ThumbPath = string.Format(@"{0}/{1}/Thumb", ParmsConfig.UpFileIPAddressUrl, now);

}

}

}

return upFileResult;

}

catch (Exception ex)

{

return new UpFileResultMessage() { IsSuccess = false, Message = ex.Message };

}

}

/// 

/// 创建指定图片文件流的缩略图

/// 

/// 缩略图文件保存路径

/// 原始文件流

/// 缩略图宽

/// 缩略图高

private void CreateThumbnail(string thumbFilePath, Stream fileStream, int width, int height)

{

using (Image image = Image.FromStream(fileStream))

{

using (Image thumbnail = image.GetThumbnailImage(width, height, null, IntPtr.Zero))

{

thumbnail.Save(thumbFilePath);

}

}

}

}

调用方式var upFileMsg = new UpFileMessage()

{

IsImage = true,

OriginalFileName = platformImg[i].FileName,

FileStream = platformImg[i].InputStream,

ThumbWidth = ThumbWidth,

Mode = "W"

};

var   upFileResultMsg = new FileUtil().UpLoadFile(upFileMsg);

代码地址:http://files.cnblogs.com/files/shanshanlaichi/%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E7%B1%BB%E5%BA%93%E5%8C%85.zip

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值