//上傳文件是否爲空
if(file1.postedfile!=null)
{
string nam = file1.postedfile.filename ;
//取得文件名裏最後一個"."的索引
int i= nam.lastindexof(".");
//取得文件擴展名
string newext =nam.substring(i);
//根據日期和文件大小不同爲文件命名,確保文件名不重複
datetime now = datetime.now;
string newname=now.dayofyear.tostring()+file1.postedfile.contentlength.tostring();
//保存文件到你所要的目錄,這裏是iis根目錄下的upload目錄.你可以改變.
//注意: 我這裏用server.mappath()取當前文件的絕對目錄.在asp.net裏""必須用""代替
file1.postedfile.saveas(server.mappath("upload"+newname+newext));
this.hyperlink1.navigateurl ="upload"+newname+newext;
//得到這個文件的相關屬性:文件名,文件類型,文件大小
//fname.text=file1.postedfile.filename;
//fenc.text=file1.postedfile.contenttype ;
//fsize.text=file1.postedfile.contentlength.tostring();
}
轉自http://www.codes51.com/article/detail_238.html