当前位置 - 股票行情交易網 - 國際漫評 - 求.net文件上傳下載代碼

求.net文件上傳下載代碼

Boolean fileOk = false;

string path = Server.MapPath("~/upload/");

if (FileUpload1.HasFile)

{

//獲取擴展名

string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();

//限定只能上傳jpg和gif圖片

string[] allowExtension = { ".jpg", ".gif", ".jpeg", ".bmp" };

for (int i = 0; i < allowExtension.Length; i++)

{

if (fileExtension == allowExtension[i])

{

fileOk = true;

break;

}

}

if (fileOk == false)

{

ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, this.GetType(), "提示!", "alert('文件類型錯誤!');", true);

}

//對上傳文件的大小進行檢測,限定文件最大不超過1M

if (FileUpload1.PostedFile.ContentLength > 1024000)

{

fileOk = false;

}

if (fileOk)

{

try

{

string pat = path + ReName(fileExtension);

ct.CardImage ="upload/" + ReName(fileExtension);

B2CBll.CardTypeManager.AddCardType(ct);

FileUpload1.PostedFile.SaveAs(pat);

ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, this.GetType(), "提示!", "alert('添加成功!');", true);

this.BindListBox();

}

catch

{

ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, this.GetType(), "提示!", "alert('上傳失敗!');", true);

}

}

else

{

ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, this.GetType(), "提示!", "alert('文件類型或者文件大小超出1M或者文件類型不對');", true);

}

}

}