先拉一個 FileUpload 元件和一個Button (上傳) 到頁面中,前台就完成了,感謝asp.net
1 |
<span style="color: #606060" id="lnum1"> 1:</span> <span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span> saveit_Click(<span style="color: #0000ff">object</span> sender, EventArgs e) <span style="color: #008000">//定義 上傳 click 事件</span> |
1 |
<span style="color: #606060" id="lnum2"> 2:</span> { |
1 |
<span style="color: #606060" id="lnum3"> 3:</span> <span style="color: #0000ff">try</span> |
1 |
<span style="color: #606060" id="lnum4"> 4:</span> { |
1 |
<span style="color: #606060" id="lnum5"> 5:</span> <span style="color: #0000ff">if</span> (savefile(FileUpload1) == <span style="color: #0000ff">true</span>) ; |
1 |
<span style="color: #606060" id="lnum6"> 6:</span> Response.Write(<span style="color: #006080">"Upload OK"</span>); |
1 |
<span style="color: #606060" id="lnum7"> 7:</span> } |
1 |
<span style="color: #606060" id="lnum8"> 8:</span> <span style="color: #0000ff">catch</span> (Exception ex) |
1 |
<span style="color: #606060" id="lnum9"> 9:</span> { |
1 |
<span style="color: #606060" id="lnum10"> 10:</span> Response.Write(<span style="color: #006080">"error code : "</span> + ex.Message); |
1 |
<span style="color: #606060" id="lnum11"> 11:</span> } |
1 |
<span style="color: #606060" id="lnum12"> 12:</span> } |
1 |
<span style="color: #606060" id="lnum13"> 13:</span> |
1 |
<span style="color: #606060" id="lnum14"> 14:</span> <span style="color: #0000ff">bool</span> savefile(FileUpload FileUpload1) |
1 |
<span style="color: #606060" id="lnum15"> 15:</span> { |
1 |
<span style="color: #606060" id="lnum16"> 16:</span> <span style="color: #0000ff">string</span> phypath = Server.MapPath(<span style="color: #006080">"~"</span>); <span style="color: #008000">//上傳的路徑加檔名</span> |
1 |
<span style="color: #606060" id="lnum17"> 17:</span> <span style="color: #0000ff">if</span> (FileUpload1.HasFile == <span style="color: #0000ff">true</span>) <span style="color: #008000">//判斷上傳物件否存在</span> |
1 |
<span style="color: #606060" id="lnum18"> 18:</span> { |
1 |
<span style="color: #606060" id="lnum19"> 19:</span> |
1 |
<span style="color: #606060" id="lnum20"> 20:</span> File.Delete(phypath); <span style="color: #008000">//重覆檔名無法上傳 , 所以使用System.IO 將檔案刪除</span> |
1 |
<span style="color: #606060" id="lnum21"> 21:</span> FileUpload1.SaveAs(phypath); <span style="color: #008000">//上傳檔案,回傳boolin</span> |
1 |
<span style="color: #606060" id="lnum22"> 22:</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>; |
1 |
<span style="color: #606060" id="lnum23"> 23:</span> } |
1 |
<span style="color: #606060" id="lnum24"> 24:</span> <span style="color: #0000ff">else</span> |
1 |
<span style="color: #606060" id="lnum25"> 25:</span> { |
1 |
<span style="color: #606060" id="lnum26"> 26:</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>; |
1 |
<span style="color: #606060" id="lnum27"> 27:</span> } |
1 |
<span style="color: #606060" id="lnum28"> 28:</span> } |
註:
1.注意伺服器檔案權限,要能夠寫入(NETWORK SERVICE)
2.重覆檔名無法寫入,先刪除再上傳
3.注意路徑,Server.MapPath 有空再來寫說明
(Visited 22 times, 1 visits today)