試用了一下asp.net 2.0的上傳控件,還是挺方便的。
分享代碼如下:
protected void button1_click(object sender, eventargs e)
{
try
{
判斷文件大小#region 判斷文件大小
int intfilelength = this.fileupload1.postedfile.contentlength;
if (intfilelength > 50000)
{
this.label1.text = "文件大于50k,不能上傳";
return;
}
#endregion
判斷保存的文件夾是否存在#region 判斷保存的文件夾是否存在
string struppath = @"upfile/" + system.datetime.now.toshortdatestring();// [email protected]"/";
//文件夾不存在的時候,創建文件夾
if (!system.io.directory.exists(server.mappath(struppath)))
{
system.io.directory.createdirectory(server.mappath(struppath));
}
string strurl = server.mappath(struppath + @"/" + this.fileupload1.filename);
#endregion
//上傳文件
this.fileupload1.saveas(strurl);
this.label1.text = "文件上傳成功";
}
catch (system.exception ex)
{
this.label1.text = "文件上傳失敗:" + ex.message;
}
}
新聞熱點
疑難解答
圖片精選