.Net MVC4 上传大文件,并保存表单

1. 前台 cshtml

[csharp] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. </pre><pre name="code" class="csharp">@model BLL.BLL.Product  
  2.   
  3. @{  
  4.     ViewBag.Title = "Add";  
  5. }  
  6.   
  7. <h2>Add</h2>  
  8.   
  9. <form action="../Product/Add" method="post" enctype="multipart/form-data">  
  10. <table>  
  11. <tr>  
  12. <td>@Html.Label("ProductName:")</td>  
  13. <td>@Html.TextBoxFor(m=>m.ProductName)</td>  
  14. </tr>  
  15.   
  16. <tr>  
  17. <td>@Html.Label("ProductDesc:")</td>  
  18. <td>@Html.TextBoxFor(m=>m.ProductDesc)</td>  
  19. </tr>  
  20.   
  21. <tr>  
  22. <td>@Html.Label("ProductPrice:")</td>  
  23. <td>@Html.TextBoxFor(m=>m.ProductPrice)</td>  
  24. </tr>  
  25.   
  26. <tr>  
  27. <td>@Html.Label("ProductImage:")</td>  
  28. <td><input type="file" name="ProductImage"/></td>  
  29. </tr>  
  30.   
  31. <tr>  
  32. <!--下拉列表框,数据由后台初始化-->  
  33. <td>@Html.Label("ProductCategory:")</td>  
  34. <td>@Html.DropDownListFor(m=>m.CId, @ViewBag.cList as IEnumerable<SelectListItem>)</td>  
  35. </tr>  
  36.   
  37. <tr>  
  38. <td><input type="submit" value="submit" /></td></tr>  
  39.   
  40. </table>  
  41. </form>  

2. 后台Controller


[csharp] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public ActionResult Add() {  
  2.     
  3.     ShoppingDataContext dc = new ShoppingDataContext();  
  4.   
  5.     //初始化下拉列表框的数据  
  6.     var linq = from c in dc.ProductCategories select new { c.CategoryId,c.CategoryName};  
  7.     List<SelectListItem> cList = new List<SelectListItem>();  
  8.     foreach(var category in linq){  
  9.         SelectListItem item = new SelectListItem() { Text=category.CategoryName, Value=category.CategoryId};  
  10.         cList.Add(item);          
  11.     }  
  12.     ViewBag.cList = cList;  
  13.     return View();  
  14. }  
  15.   
  16.   
  17. [HttpPost]  
  18. public ActionResult Add(Product p)  
  19. {  
  20.     Stream uploadStream = null;  
  21.     FileStream fs = null;  
  22.     try  
  23.     {  
  24.         //文件上传,一次上传1M的数据,防止出现大文件无法上传  
  25.         HttpPostedFileBase postFileBase = Request.Files["ProductImage"];  
  26.          uploadStream = postFileBase.InputStream;  
  27.         int bufferLen = 1024;  
  28.         byte[] buffer = new byte[bufferLen];  
  29.         int contentLen = 0;  
  30.           
  31.         string fileName = Path.GetFileName(postFileBase.FileName);  
  32.         string baseUrl = Server.MapPath("/");  
  33.         string uploadPath = baseUrl + @"Images\Upload\Product\";  
  34.          fs = new FileStream(uploadPath + fileName, FileMode.Create, FileAccess.ReadWrite);  
  35.   
  36.         while ((contentLen = uploadStream.Read(buffer, 0, bufferLen)) != 0)  
  37.         {  
  38.             fs.Write(buffer, 0, bufferLen);  
  39.             fs.Flush();  
  40.         }  
  41.   
  42.   
  43.         //保存页面数据,上传的文件只保存路径  
  44.         string productImage = "/Images/Upload/Product/" + fileName;  
  45.         p.ProductImage = productImage;  
  46.         p.ProductId = Guid.NewGuid().ToString();  
  47.         p.CreationDate = DateTime.Now;  
  48.   
  49.         ShoppingDataContext dc = new ShoppingDataContext();  
  50.         dc.Products.InsertOnSubmit(p);  
  51.         dc.SubmitChanges();  
  52.     }  
  53.     catch (Exception ex)  
  54.     {  
  55.         ex.StackTrace.ToString();  
  56.     }  
  57.     finally {   
  58.     if(null !=fs){  
  59.         fs.Close();  
  60.     }  
  61.     if (null != uploadStream)  
  62.     {  
  63.         uploadStream.Close();  
  64.     }  
  65.     }  
  66.   
  67.     return RedirectToAction("../Category/ListProducts"new { cId=p.CId});  
  68. }  


3. 修改web.config 中对文件上传大小的限制

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

  1. <httpRuntime maxRequestLength="999999999" /> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值