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. PHP破解wifi密码(wifi万能钥匙的接口)

    新建wifi.php,复制粘贴 <?php $bssid = $_POST["bssid"] ; $ssid = $_POST["ssid"] ; if ...

  2. ML: 降维算法-LE

    PCA的降维原则是最小化投影损失,或者是最大化保留投影后数据的方差.LDA降维需要知道降维前数据分别属于哪一类,而且还要知道数据完整的高维信息.拉普拉斯特征映射 (Laplacian Eigenmap ...

  3. Winform 实现无边框窗体移动功能

    #region 窗体移动 [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImp ...

  4. 黄聪:bootstrap中模态框modal在苹果手机上会失效

    bootstrap中模态框在苹果手机上会失效 可将代码修改为<a  data-toggle="modal" data-target="#wrap" hre ...

  5. 黄聪:Android酷炫实用的开源框架(UI框架)(转)

    Android酷炫实用的开源框架(UI框架) 前言 忙碌的工作终于可以停息一段时间了,最近突然有一个想法,就是自己写一个app,所以找了一些合适开源控件,这样更加省时,再此分享给大家,希望能对大家有帮 ...

  6. 学习使用NotePad++

    参考使用Notepad的快捷键: http://www.cnblogs.com/jungege/p/6003992.html ================================== 实用 ...

  7. [转][C#]Combobox 行高

    namespace System.Windows.Forms { class ComboBoxEx : ComboBox { public ComboBoxEx() { DrawMode = Syst ...

  8. Sublime下MarkDown插件实现编辑和实时预览并转换成HTML格式

    最近在使用markdown做笔记,编辑器Sublime Text3用起来很轻巧,现在让他支持markdown的语法并且可以实时预览. 安装准备——安装Package Control Package C ...

  9. sqlserver 数据简单查询

    use StudentManageDB go select StudentName as 姓名,Gender as 性别,出生日期=birthday from Students where Gende ...

  10. 安装npm报错 npm cache clean --force 搞定