asp.net上傳execl文件后,在頁面上加載顯示(示例代碼)
#region 上傳Execl文件
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string NewFileName = string.Empty;
string ErrorMess = UpLoadFile(FileUpload1, ".xls|.xlsx", 1024 * 5, Server.MapPath("/Report/SocialApply/"), 1, out NewFileName);
if (string.IsNullOrEmpty(ErrorMess))
{
Label1.Text = "√文件上傳成功";
ViewState["UpLoadFile"] = "/Report/SocialApply/" + NewFileName;
try
{
FileStream file = new FileStream
(Server.MapPath(ViewState["UpLoadFile"] as string),
FileMode.Open, FileAccess.Read,
FileShare.ReadWrite);
DataTable dt1 = FairHR.Util.XmlExcelReport.ReadExcelToDataTable(file, 0, 0);
ViewState.Add("ViewDT", dt1);
GridView1.DataSource = ViewState["ViewDT"] as DataTable;
GridView1.DataBind();
file.Close();
//Maticsoft.Common.MessageBox.ResponseScript(Page, "$.messager.alert('系統(tǒng)提示', '操作成功!', 'info');");
}
catch
{
Maticsoft.Common.MessageBox.ResponseScript(Page, "$.messager.alert('系統(tǒng)提示', '請重新上傳Execl文件再操作', 'warning');");
}
}
else
{
Label1.Text = "×文件上傳失敗";
}
}
else
{
Label1.Text = "×請先選擇上傳文件";
}
ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "idAlert1", "closeLoad();", true);
}
#endregion
#region 上傳文件
/// <summary>
/// 上傳文件
/// </summary>
/// <param name="fu">上傳文件</param>
/// <param name="type">上傳文件類型,例如.jpg|.gif|.bmp</param>
/// <param name="size">限制上傳文件大小,單位為k</param>
/// <param name="path">上傳路徑,需使用server.mappath</param>
/// <param name="nametype">1為自動命名,0用原名</param>
/// <returns></returns>
public static string UpLoadFile(FileUpload fu, string type, int size, string path, int nametype, out string newFileName)
{
newFileName = null;
string erorr = null;
int Size = fu.PostedFile.ContentLength / 1024;
if (Size > size)
{
erorr = "上傳文件太大!";
return erorr;
}
string Type = fu.FileName;
if (Type.IndexOf(".") == -1) { erorr = "上傳文件類型有誤!"; return erorr; }
Type = Type.Substring(Type.LastIndexOf(".")).ToUpper();
type = type.ToUpper();
if (type.IndexOf(Type) == -1) { erorr = "上傳文件類型有誤!"; return erorr; }
string filename = "";
if (nametype == 1)
{
string nowdate = DateTime.Now.ToString();
nowdate = nowdate.Replace(":", "").Replace(" ", "").Replace("-", "").Trim();
Random r = new Random();
int a = r.Next(1000);
filename = nowdate + a.ToString() + Type;
newFileName = filename;
}
else
{
filename = fu.FileName;
if (System.IO.File.Exists(path + filename)) { erorr = "此文件名已經(jīng)存在!"; return erorr; }
}
fu.SaveAs(path + filename);
return erorr;//返回有錯的錯誤信息,沒有錯誤返回null
}
#endregion
相關(guān)文章
ASP.NET下使用WScript.Shell執(zhí)行命令
ASP.NET下有自己的執(zhí)行CMD命令的方式,這里用WScript.Shell似有畫蛇添足之嫌,但是我們也不能排除真的有機器禁用了.NET的相關(guān)類,未雨綢繆嘛。當(dāng)然也不僅僅局限于WScript.Shell,只要是ASP中能用的組件,統(tǒng)統(tǒng)都可以用于ASP.NET中,而且還更方便!2008-05-05.NET Core跨平臺串口通訊使用SerialPortStream基礎(chǔ)類庫問題解決
這篇文章介紹了.NET Core跨平臺串口通訊使用SerialPortStream基礎(chǔ)類庫問題解決,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01.Net使用分表分庫框架ShardingCore實現(xiàn)多字段分片
本文詳細講解了.Net使用分表分庫框架ShardingCore實現(xiàn)多字段分片的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12.NET MVC中ViewData,ViewBag和TempData的區(qū)別淺析
這篇文章主要介紹了.NET MVC中ViewData,ViewBag和TempData的區(qū)別,分析了ViewData,ViewBag和TempData在賦值、功能特性等方面的區(qū)別于用法,具有一定參考借鑒價值,需要的朋友可以參考下2015-01-01.net core EF Core調(diào)用存儲過程的方式
這篇文章主要給大家介紹了關(guān)于.net core EF Core調(diào)用存儲過程的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用.net core EF Core具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05詳解.NET中的加密算法總結(jié)(自定義加密Helper類續(xù))
這篇文章主要介紹了詳解.NET中的加密算法總結(jié)(自定義加密Helper類續(xù)) ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-12-12