ASP.NET MVC- Upload File的例子
生活随笔
收集整理的這篇文章主要介紹了
ASP.NET MVC- Upload File的例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
上傳文件是一項基本功能,一定要了解的。先來看一下使用ASP.NET MVC實現簡單的上傳。
一、簡單的例子
Controller的例子
public ActionResult UploadDemo(){return View();}public ActionResult FileUpload(){HttpPostedFileBase file = Request.Files["file"];if (file != null){string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(file.FileName));file.SaveAs(filePath);Response.Write("<script>alert('上傳成功');location.href='/Index/UploadDemo' </script>");}else{Response.Write("<script>alert('上傳失敗');location.href='/Index/UploadDemo' </script>");}return null;}對應的View視圖
@{Layout = null; } <!DOCTYPE html> <html> <head><title>UploadDemo</title> </head> <body><div><h2>上傳例子</h2>@using (Html.BeginForm("FileUpload", "Index", FormMethod.Post, new { enctype = "multipart/form-data" })){<input type="file" name="file" /><input type="submit" value="uploadFile" />}</div> </body> </html>?
轉載于:https://www.cnblogs.com/cxeye/p/4988604.html
總結
以上是生活随笔為你收集整理的ASP.NET MVC- Upload File的例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IBatis.net动态SQL语句
- 下一篇: iOS点滴- ViewControlle