#眉標=ASP.NET、MVC、Url-Routing #副標=ASP.NET MVC Framework開發與應用(3) #大標=ASP.NET MVC的檔案上傳與下載 #作者=文/圖 董大偉 ===<反灰>============= Return View ( [ viewName [ , masterName [ , Model ] ] ] ) ================ ============= 程式1 Function Download(ByVal ID As String) As ActionResult   Return File(Request.MapPath("~\UploadFiles\") & ID, "Zip") End Function ================ ===<反灰>============= Return File("檔案路徑") Return File(bytes()) Return File(FileStream物件) ================ ============= 程式2

檔案: <% =ViewData("msg")%>

================ ============= 程式3 _ Function Upload(FormData As FormCollection ) As ActionResult Dim msg As String = "" For i As Integer = 0 To Request.Files.Count - 1 '顯示訊息(放入Msg) msg += "
" & Request.Files(i).FileName & "...已上傳" '儲存檔案 Request.Files(i).SaveAs(Request.MapPath("~\UploadFiles\") & Request.Files(i).FileName) Next '將msg傳遞給ViewPage ViewData("msg") = msg Return View() End Function ================ ===<反灰>============= http://localhost:57249/file/download/[檔名] ================ ============= 程式4:Global.asax.vb Shared Sub RegisterRoutes(ByVal routes As RouteCollection) routes.IgnoreRoute("{resource}.axd/{*pathInfo}") routes.MapRoute("Blog", "{Archive}/{EntryDate}", New With {.controller = "Archive", .action = "Entry"}) routes.MapRoute( _ "Default", _ "{controller}/{action}/{id}", _ New With {.controller = "Home", .action = "Index", .id = ""} _ ) End Sub ================ ===<反灰>============= http://localhost:50522/archive/2009-06-13 ================ ============= 程式5 Function Entry(ByVal entryDate As DateTime) (…略…) Return View() End Function ================ ===<反灰>============= http://localhost:53339/Product/Details/123 ================ ===<反灰>============= http://localhost:53339/Product/Details/AK47 ================ ===<反灰>============= Function Details(ByVal productId As Integer) As ActionResult (…略…) End Function ================ ===<反灰>============= routes.MapRoute( "Product", "Product/{productId}", New With {.controller = "Product", .action = "Details"}, New With {.productId = "\d+"}) ================ =====線上影音教學 ========== ●RUN!PC網站:www.runpc.com.tw/content/main_content.aspx?mgo=187&fid=E04 ●作者BLOG:blog.studyhost.com ==========