ASP.NET MVC HttpPostedFileBase文件上傳的實(shí)例代碼
本文介紹了ASP.NET MVC HttpPostedFileBase文件上傳 ,分享給大家,希望對(duì)大家有幫助
HttpPostedFileBase文件上傳,支持多文件一次上傳,如有圖片,則支持略縮圖保存
文件傳輸信息封裝
/// <summary>
/// 文件生成方式
/// </summary>
public class UpFileMessage
{
/// <summary>
/// 文件名
/// </summary>
public string OriginalFileName { get; set; }
/// <summary>
/// 是否保存略縮圖
/// </summary>
public bool IsImage { get; set; }
/// <summary>
/// 文件流
/// </summary>
public Stream FileStream { get; set; }
/// <summary>
/// 生成縮略圖的方式
/// [WH]-指定寬高
/// [H]-指定高,按比例縮放寬
/// [W]-指定寬,按比例縮放高
/// </summary>
public string Mode { get; set; }
/// <summary>
/// 略縮圖高度
/// </summary>
public int? ThumbHeight { get; set; }
/// <summary>
/// 略縮圖寬度
/// </summary>
public int? ThumbWidth { get; set; }
}
文件上傳返回結(jié)果
/// <summary>
/// 文件上傳返回結(jié)果
/// </summary>
public class UpFileResultMessage
{
/// <summary>
/// 文件保存是否成功
/// </summary>
public bool IsSuccess { get; set; }
/// <summary>
/// 錯(cuò)誤消息
/// </summary>
public string Message { get; set; }
/// <summary>
/// 原始文件名-(無擴(kuò)展名)
/// </summary>
public string FileName { get; set; }
/// <summary>
/// 文件名擴(kuò)展名
/// </summary>
public string FileSuffix { get; set; }
/// <summary>
/// 文件名保存路徑
/// </summary>
public string FilePath { get; set; }
/// <summary>
/// 文件類型為圖片時(shí)
/// 縮略圖保存路徑
/// </summary>
public string ThumbPath { get; set; }
/// <summary>
/// 保存文件名(無擴(kuò)展名)
/// </summary>
public string SaveFileName { get; set; }
/// <summary>
/// 文件自增ID
/// </summary>
public int[] FileIdArray { get; set; }
}
文件上傳類庫
需引用System.Web命名空間,并對(duì) [System.Web.UI.Page] 進(jìn)行繼承,調(diào)用Server.MapPath方法
public class FileUtil : System.Web.UI.Page
{
/// <summary>
/// 圖片上傳
/// </summary>
/// <param name="fileMessage">文件生成方式</param>
/// <returns></returns>
public UpFileResultMessage UpLoadFile(UpFileMessage fileMessage)
{
try
{
string now = DateTime.Today.ToString("yyyyMMdd");
string guid = Guid.NewGuid().ToString();
//獲取文件擴(kuò)展名
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);
}
}
//上傳文件為圖片時(shí),需創(chuàng)建縮略圖
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 };
}
}
/// <summary>
/// 創(chuàng)建指定圖片文件流的縮略圖
/// </summary>
/// <param name="thumbFilePath">縮略圖文件保存路徑</param>
/// <param name="fileStream">原始文件流</param>
/// <param name="width">縮略圖寬</param>
/// <param name="height">縮略圖高</param>
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);
}
}
}
}
調(diào)用方式
var upFileMsg = new UpFileMessage()
{
IsImage = true,
OriginalFileName = platformImg[i].FileName,
FileStream = platformImg[i].InputStream,
ThumbWidth = ThumbWidth,
Mode = "W"
};
var upFileResultMsg = new FileUtil().UpLoadFile(upFileMsg);
代碼地址:文件上傳類庫包.zip
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET MVC 從IHttp到頁面輸出的實(shí)例代碼
- asp.net mvc路由篇 如何找到 IHttpHandler方法介紹
- ASP.NET MVC Web API HttpClient簡介
- ASP.NET頁面之間傳值的方式之Application實(shí)例詳解
- ASP.NET C#中Application的用法教程
- ASP.NET 中的Application詳解
- ASP.NET中使用Application對(duì)象實(shí)現(xiàn)簡單在線人數(shù)統(tǒng)計(jì)功能
- Asp.net MVC中的Http管道事件為什么要以Application_開頭(原因解析)
相關(guān)文章
Asp.net操作Excel更輕松的實(shí)現(xiàn)代碼
今天先介紹一個(gè)關(guān)于導(dǎo)出數(shù)據(jù)的例子,以Excel為模板。直接進(jìn)入正題了2011-10-10
手動(dòng)把a(bǔ)sp.net的類生成dll文件的方法
當(dāng)我們?cè)陂_發(fā)的時(shí)候,有時(shí)會(huì)將一些方法封裝起來供別人調(diào)用,下面就是一種生成DLL的方法.2009-11-11
asp.net gridview分頁:第一頁 下一頁 1 2 3 4 上一頁 最末頁
這篇文章主要介紹了asp.net gridview分頁:第一頁 下一頁 1 2 3 4 上一頁 最末頁,可使用上下鍵選中行,選中后點(diǎn)擊修改,textbox獲得gridview中的代碼的數(shù)據(jù),需要的朋友可以參考下2014-12-12
輕量級(jí)ORM框架Dapper應(yīng)用之實(shí)現(xiàn)In操作
這篇文章介紹了使用Dapper實(shí)現(xiàn)In操作的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
解讀ASP.NET密碼強(qiáng)度驗(yàn)證代碼實(shí)例分享
這篇文章介紹了ASP.NET密碼強(qiáng)度驗(yàn)證代碼實(shí)例,有需要的朋友可以參考一下2013-10-10

