c#實現按日期命名上傳文件代碼做開發的應該能夠用得了吧!
附件說明邊的TextBoxId=TextBox
浏覽Id=UpLoadFile
上傳Id=AddFile
文件目錄wwwroot/myWeb/userfiles/upload
private void AddFile_Click(object sender SystemEventArgs e)
{
if(UpLoadFilePostedFileFileNameTrim()!=)
{
String fileName =UpLoadFilePostedFileFileNameSubstring (UpLoadFilePostedFileFileNameLastIndexOf(\\)+ UpLoadFilePostedFileFileNameLength UpLoadFilePostedFileFileNameLastIndexOf(\\))
///取到當前時間的年月日分秒和毫秒的值並使用字符串格式把它們組合成一個字符串
String fileTime = DateTimeNowYearToString() + DateTimeNowMonthToString()
+ DateTimeNowDayToString() + DateTimeNowHourToString()
+ DateTimeNowSecondToString() + DateTimeNowMinuteToString()
+ DateTimeNowMillisecondToString()
///在時間字符串後面添加一個隨機數和文件的後綴名
String src=fileNameSubstring(fileNameLastIndexOf()+fileNameLengthfileNameLastIndexOf())ToLower()
if(srcToLower()==rar || srcToLower()==doc || srcToLower()==xls)
{
fileName =fileTime + GetRandomint() + + src
///上載文件到服務器硬盤
UpLoadFilePostedFileSaveAs(ServerMapPath(RequestApplicationPath) + \\userfiles\\upload\\ + fileName)
if(TextBoxTextTrim()==)
{
FCKeditorValue=FCKeditorValue+<a href=userfiles/upload/+fileName+>下載附件</a><br>
}
else
{
FCKeditorValue=FCKeditorValue+<a href=userfiles/upload/+fileName+>+TextBoxTextToString()+< /a><br>
}
}
else
{
ResponseWrite(<script>alert(\文件格式不正確請上傳格式為RAR的文件!\)</script>)
}
}
}
private String GetRandomint()
{
Random random = new Random()
return(randomNext()ToString()) //產生一個小於的隨機正整數
}
From:http://tw.wingwit.com/Article/program/net/201311/15619.html