ASP.NET MVC上傳檔案

來源:互聯網
上載者:User

標籤:art   3.2   info   body   query   get   text   load   space   

最近參考網路資料,學習了ASP.NET MVC如何上傳檔案。最基本的,沒有用jQuery等技術。

1、定義Model

    public class TestModel
    {
        [Display(Name = "標題")]
        [Required]
        public string Title
        {
            get;
            set;
        }
        [Display(Name = "內容")]
        [Required]
        [DataType(DataType.MultilineText)]
        public string Content
        {
            get;
            set;
        }
        public string AttachmentPath
        {
            get;
            set;
        }
    }

2、Controller

1234567891011121314151617181920212223242526272829303132333435363738394041 public class TestController : Controller{    //    // GET: /Test/     public ActionResult Upload()    {        return View();    }    /// <summary>    /// 提交方法    /// </summary>    /// <param name="tm">模型資料</param>    /// <param name="file">上傳的檔案對象,此處的參數名稱要與View中的上傳標籤名稱相同</param>    /// <returns></returns>    [HttpPost]    public ActionResult Upload(TestModel tm, HttpPostedFileBase file)    {        if (file == null)        {            return Content("沒有檔案!", "text/plain");        }        var fileName = Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(file.FileName));        try        {            file.SaveAs(fileName);            //tm.AttachmentPath = fileName;//得到全部model資訊            tm.AttachmentPath = "../upload/" + Path.GetFileName(file.FileName);          //return Content("上傳成功!", "text/plain");            return RedirectToAction("Show",tm);        }        catch        {            return Content("上傳異常 !", "text/plain");        }    }    public ActionResult Show(TestModel tm)    {        return View(tm);    }}

 3、View

3.1 Upload.cshtml

@model MvcApplication1.Models.TestModel @{    ViewBag.Title = "Upload";} <!DOCTYPE html><html><head>    <title>普通上傳</title></head><body>    @*enctype= "multipart/form-data"是必需有的,否則action接收不到相應的file*@    @using (Html.BeginForm("Upload", "Test", FormMethod.Post, new { enctype = "multipart/form-data" }))    {        @Html.LabelFor(mod => mod.Title)        <br />        @Html.EditorFor(mod => mod.Title)        <br />     <br />        @Html.LabelFor(mod => mod.Content)        <br />        @Html.EditorFor(mod => mod.Content)        <br />        <span>上傳檔案</span>        <br />        <input type="file" name="file" />        <br />        <br />        <input id="ButtonUpload" type="submit" value="提交" />    }</body></html>

 3.2 Show.cshtml

@model MvcApplication1.Models.TestModel @{    ViewBag.Title = "Show";} <h2>Show</h2>@Html.LabelFor(mod => mod.Title)<br />@Html.EditorFor(mod => mod.Title)<br />     <br />@Html.LabelFor(mod => mod.Content)<br />@Html.EditorFor(mod => Model.Content)<br />圖片:<img src="@Model.AttachmentPath" alt="img" />

ASP.NET MVC上傳檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.