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

本文档介绍了如何在.Net MVC4中实现大文件上传,并详细阐述了前台cshtml页面与后台Controller的交互过程,通过修改Web配置以支持大文件上传。
摘要由CSDN通过智能技术生成

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>

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});
        }


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

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

    <httpRuntime maxRequestLength="999999999" />



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sust2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值