1. 程式人生 > 實用技巧 >ASP.NET 上傳檔案方法

ASP.NET 上傳檔案方法

一、ASP.NET MVC 實現上傳下載

1.首先是Form表單提交

前臺:

 <form action='@Url.Action("Upload", "File")' method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="提交" />
</form>

注意form標籤已經包括了enctype標籤,而method屬性則設為”post”,這樣設定並不多於因為預設的提交時通過HTTP get方式進行的。

後臺:

 public ActionResult Upload(HttpPostedFileBase file)
            {
                var fileName = file.FileName;
                var filePath = Server.MapPath(string.Format("~/{0}", "File"));
                file.SaveAs(Path.Combine(filePath, fileName));
                return Content("上傳成功");
            }

2.前臺:

a href='@Url.Action("Download", "File", new { fileName = "323152.jpg" })'>下載檔案</a>