.Net MVC4 上傳大檔案,並儲存表單

來源:互聯網
上載者:User

標籤:conf   --   value   rom   new   ipa   action   cep   ext   

1. 前台 cshtml

</pre><pre name="code" class="csharp">@model BLL.BLL.Product    @{      ViewBag.Title = "Add";  }    <h2>Add</h2>    <form action="../Product/Add" method="post" enctype="multipart/form-data">  <table>  <tr>  <td>@Html.Label("ProductName:")</td>  <td>@Html.TextBoxFor(m=>m.ProductName)</td>  </tr>    <tr>  <td>@Html.Label("ProductDesc:")</td>  <td>@Html.TextBoxFor(m=>m.ProductDesc)</td>  </tr>    <tr>  <td>@Html.Label("ProductPrice:")</td>  <td>@Html.TextBoxFor(m=>m.ProductPrice)</td>  </tr>    <tr>  <td>@Html.Label("ProductImage:")</td>  <td><input type="file" name="ProductImage"/></td>  </tr>    <tr>  <!--下拉式清單方塊,資料由後台初始化-->  <td>@Html.Label("ProductCategory:")</td>  <td>@Html.DropDownListFor(m=>m.CId, @ViewBag.cList as IEnumerable<SelectListItem>)</td>  </tr>    <tr>  <td><input type="submit" value="submit" /></td></tr>    </table>  </form>  
View Code

 

 2. 後台Controller

public ActionResult Add() {          ShoppingDataContext dc = new ShoppingDataContext();        //初始化下拉式清單方塊的資料      var linq = from c in dc.ProductCategories select new { c.CategoryId,c.CategoryName};      List<SelectListItem> cList = new List<SelectListItem>();      foreach(var category in linq){          SelectListItem item = new SelectListItem() { Text=category.CategoryName, Value=category.CategoryId};          cList.Add(item);              }      ViewBag.cList = cList;      return View();  }      [HttpPost]  public ActionResult Add(Product p)  {      Stream uploadStream = null;      FileStream fs = null;      try      {          //檔案上傳,一次上傳1M的資料,防止出現大檔案無法上傳          HttpPostedFileBase postFileBase = Request.Files["ProductImage"];           uploadStream = postFileBase.InputStream;          int bufferLen = 1024;          byte[] buffer = new byte[bufferLen];          int contentLen = 0;                    string fileName = Path.GetFileName(postFileBase.FileName);          string baseUrl = Server.MapPath("/");          string uploadPath = baseUrl + @"Images\Upload\Product\";           fs = new FileStream(uploadPath + fileName, FileMode.Create, FileAccess.ReadWrite);            while ((contentLen = uploadStream.Read(buffer, 0, bufferLen)) != 0)          {              fs.Write(buffer, 0, bufferLen);              fs.Flush();          }              //儲存頁面資料,上傳的檔案只儲存路徑          string productImage = "/Images/Upload/Product/" + fileName;          p.ProductImage = productImage;          p.ProductId = Guid.NewGuid().ToString();          p.CreationDate = DateTime.Now;            ShoppingDataContext dc = new ShoppingDataContext();          dc.Products.InsertOnSubmit(p);          dc.SubmitChanges();      }      catch (Exception ex)      {          ex.StackTrace.ToString();      }      finally {       if(null !=fs){          fs.Close();      }      if (null != uploadStream)      {          uploadStream.Close();      }      }        return RedirectToAction("../Category/ListProducts", new { cId=p.CId});  }  
View Code

 

 3. 修改web.config 中對檔案上傳大小的限制

在 <system.web></system.web> 直接增加如下:

<httpRuntime maxRequestLength="999999" />  

 

 

 

 原文連結

.Net MVC4 上傳大檔案,並儲存表單

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.