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 = ;
byte[] buffer = new byte[bufferLen];
int contentLen = ; 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, , bufferLen)) != )
{
fs.Write(buffer, , 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="" />  

【转】 .Net MVC4 上传大文件,并保存表单的更多相关文章

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

    1. 前台 cshtml </pre><pre name="code" class="csharp">@model BLL.BLL.Pr ...

  2. [Asp.net]Uploadify上传大文件,Http error 404 解决方案

    引言 之前使用Uploadify做了一个上传图片并预览的功能,今天在项目中,要使用该插件上传大文件.之前弄过上传图片的demo,就使用该demo进行测试.可以查看我的这篇文章:[Asp.net]Upl ...

  3. php 上传大文件配置upload_max_filesize和post_max_size选项

    php 上传大文件配置upload_max_filesize和post_max_size选项 (2014-04-29 14:42:11) 转载▼ 标签: php.ini upload _files[f ...

  4. PHP上传大文件 分割文件上传

    最近遇到这么个情况,需要将一些大的文件上传到服务器,我现在拥有的权限是只能在一个网页版的文件管理系统来进行操作,可以解压,可以压缩,当然也可以用它来在线编辑.php文件. 文件有40M左右,但是服务器 ...

  5. ASP.NET上传大文件的问题

    原文:http://www.cnblogs.com/wolf-sun/p/3657241.html?utm_source=tuicool&utm_medium=referral 引言 之前使用 ...

  6. php 上传大文件主要涉及配置upload_max_filesize和post_max_size两个选项

    php 上传大文件主要涉及配置 upload_max_filesize 和post_max_size两个选项   今天在做上传的时候出现一个非常怪的问题,有时候表单提交可以获取到值,有时候就获取不到了 ...

  7. SWFUpload上传大文件(暂时用用,真正用的时候还是要改的)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. PHP上传大文件和处理大数据

    1. 上传大文件 /* 以1.5M/秒的速度写入文件,防止一次过写入文件过大导致服务器出错(chy/20150327) */ $is_large_file = false; if( strlen($x ...

  9. QQ上传大文件为什么这么快

    今天和同事在群里讨论“QQ上传大文件/QQ群发送大文件时,可以在极短的时间内完成”是如何做到的. 有时候我们通过QQ上传一个几百M的文件,竟然只用了几秒钟,从带宽上限制可以得出,实际上传文件是不可能的 ...

随机推荐

  1. Hadoop的单机模式、伪分布式模式和完全分布式模式

    1.单机(非分布式)模式 这种模式在一台单机上运行,没有分布式文件系统,而是直接读写本地操作系统的文件系统. 2.伪分布式运行模式 这种模式也是在一台单机上运行,但用不同的Java进程模仿分布式运行中 ...

  2. 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---Crawl

    阅读OReilly.Web.Scraping.with.Python.2015.6笔记---Crawl 1.函数调用它自身,这样就形成了一个循环,一环套一环: from urllib.request ...

  3. Delphi实现软件中登录用户的操作权限

    数据库结构:包括两张表BaseData和UserRightData,BaseData中是一张基本表,里面不区分用户,UserRightData是用户权限表,结构和BaseData一样,只是多了用户字段 ...

  4. RichEdit选中文字右键菜单的实现

    procedure TForm1.RichEdit1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: ...

  5. [Android] JNI中的Local Reference

    参考文章:<在 JNI 编程中避免内存泄漏> 一.Local Reference 深层解析 JNI Local Reference 的生命期是在 native method 的执行期(从 ...

  6. vue中绑定多个class,多个情况使用同一个class时,用js的或 “||”

    class中 当依赖中断的时候是绑定的red,但是这样写绑定不了, 大括号{}里可以下js代码的,下面的orange就可以绑定成功,写成双竖线 形式.

  7. T-SQL select语句连接两个表

    当一个表中按条件出现多个记录时,会按照匹配条件生成多个结果记录.left out 和right out 是对不能匹配的记录发生作用.

  8. Unable to load native-hadoop library for your platform... using builtin-java classes where applicable(四十四)

    问题描述: Unable to load native-hadoop library for your platform... using builtin-java classes where app ...

  9. 《Java并发编程实战》笔记-Happens-Before规则

    Happens-Before规则 程序顺序规则.如果程序中操作A在操作B之前,那么在线程中A操作将在B操作之前执行. 监视器锁规则.在监视器锁上的解锁操作必须在同一个监视器锁上的加锁操作之前执行. v ...

  10. dos命令及github介绍

    dos命令:(不区分大小写)(尽量不要用汉字) 1.打开终端的快捷方式: window+r 输入cmd 或点击 开始栏 输入cmd 2.终端的目录:c盘默认 user/administator: 想在 ...