【转】 .Net MVC4 上传大文件,并保存表单
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> 直接增加如下:
【转】 .Net MVC4 上传大文件,并保存表单的更多相关文章
- .Net MVC4 上传大文件,并保存表单
1. 前台 cshtml </pre><pre name="code" class="csharp">@model BLL.BLL.Pr ...
- [Asp.net]Uploadify上传大文件,Http error 404 解决方案
引言 之前使用Uploadify做了一个上传图片并预览的功能,今天在项目中,要使用该插件上传大文件.之前弄过上传图片的demo,就使用该demo进行测试.可以查看我的这篇文章:[Asp.net]Upl ...
- 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 ...
- PHP上传大文件 分割文件上传
最近遇到这么个情况,需要将一些大的文件上传到服务器,我现在拥有的权限是只能在一个网页版的文件管理系统来进行操作,可以解压,可以压缩,当然也可以用它来在线编辑.php文件. 文件有40M左右,但是服务器 ...
- ASP.NET上传大文件的问题
原文:http://www.cnblogs.com/wolf-sun/p/3657241.html?utm_source=tuicool&utm_medium=referral 引言 之前使用 ...
- php 上传大文件主要涉及配置upload_max_filesize和post_max_size两个选项
php 上传大文件主要涉及配置 upload_max_filesize 和post_max_size两个选项 今天在做上传的时候出现一个非常怪的问题,有时候表单提交可以获取到值,有时候就获取不到了 ...
- SWFUpload上传大文件(暂时用用,真正用的时候还是要改的)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- PHP上传大文件和处理大数据
1. 上传大文件 /* 以1.5M/秒的速度写入文件,防止一次过写入文件过大导致服务器出错(chy/20150327) */ $is_large_file = false; if( strlen($x ...
- QQ上传大文件为什么这么快
今天和同事在群里讨论“QQ上传大文件/QQ群发送大文件时,可以在极短的时间内完成”是如何做到的. 有时候我们通过QQ上传一个几百M的文件,竟然只用了几秒钟,从带宽上限制可以得出,实际上传文件是不可能的 ...
随机推荐
- Hystrix 详细说明
在 Hystrix 入门中,使用 Hystrix 时创建命令并给予执行,实际上 Hystrix 有一套较为复杂的执行逻辑,简单来说明以下运作流程: 在命令开始执行时,会做一些准备工作,例如为命令创建响 ...
- Rectified/无限流量/KVM/1G内存/亚洲优化/月付3.99刀起/商家首次续费优惠/91yun第600篇博文
具体配置如下: 1v CPU + 1G 内存 + 10G SSD + 无限流量( 30T ) + 100M 口 + FreeBSD 支持 + 1IPv4 + IPv6 (可工单) + rDNS = 3 ...
- 银行卡所属公司判断 参考自https://blog.csdn.net/well2049/article/details/79429130
在网上找到了一个银行卡的验证,通过阿里的支付宝接口进行校验,能够准确识别是否存在,归属行,卡号类型是储蓄卡(DC)还是信用卡(CC). 接口api:需要传入的2个参数,卡号cardNo和cardBin ...
- Linux磁盘监控工具说明
1.1 iostat 系统systat包里的工具,以kB/s为单位统计,2表示以2秒为频率统计一次:iostat –x –k 2 10000 rrqm/s:每秒这个设备相关的读取请求有多少被Merge ...
- Hiero的spreadsheet中添加tag属性列
Hiero在对剪辑线上的item进行管理的时候,往往会添加能多tag,而在管 理面板spreadsheet中却无法对tag进行查询,这是一件很麻烦的事,Hiero Development Guide中 ...
- LeetCode——150. Evaluate Reverse Polish Notation
一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...
- WhereHows前后端配置文件
前端: # This is the main configuration file for the application. # ~~~~~ # Secret key # ~~~~~ # The se ...
- Mongodb条件查询Query的用法
Query.All("name", "a", "b");//通过多个元素来匹配数组Query.And(Query.EQ("name ...
- Visual Studio 2010 Shortcut
General Shortcut Description Ctrl-X or Shift-Delete Cuts the currently selected item to the clipboar ...
- 服务网关zuul之三:zuul统一异常处理
我们详细介绍了Spring Cloud Zuul中自己实现的一些核心过滤器,以及这些过滤器在请求生命周期中的不同作用.我们会发现在这些核心过滤器中并没有实现error阶段的过滤器.那么这些过滤器可以用 ...