• 【转】 .Net MVC4 上传大文件,并保存表单


    1前台:cshtml

     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

     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 + @"ImagesUploadProduct";  
    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" />  
  • 相关阅读:
    httpd添加新模块
    编译httpd细节
    apache配置文件说明及一些指令
    xen原理
    EXSI的使用
    VMWare ESX server安装
    虚拟化技术
    Kvm命令集管理虚拟机
    RAID几种方式
    BZOJ1011 [HNOI2008]遥远的行星 【奇技淫巧】
  • 原文地址:https://www.cnblogs.com/mingjing/p/5316185.html
Copyright © 2020-2023  润新知