【转】 .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的文件,竟然只用了几秒钟,从带宽上限制可以得出,实际上传文件是不可能的 ...
随机推荐
- Redis:五种数据类型的简单增删改查
Redis简单增删改查例子 例一:字符串的增删改查 #增加一个key为ay_key的值 127.0.0.1:6379> set ay_key "ay" OK #查询ay_ke ...
- DelphiXE5如何获取Android手机SIM卡串号[转]
手机号不是存在SIM卡上的,SIM卡只有一个串号.在运营商的服务器上有手机号和序号对应的一张表.所以你SIM卡办理遗失,运营商只要把新SIM卡的串号和你原来的手机号绑定即可. 获取手机号的唯一办法是收 ...
- 自定义Write节点的beforerender属性
由于nuke中的write节点提供了beforerender,afterrender这类事件,我们想添加一些功能只需要在这里面敲入代码即可.事件一旦发生,自然会触发我们敲入的code. Write ...
- Speeding Up The Traveling Salesman Using Dynamic Programming
Copied From:https://medium.com/basecs/speeding-up-the-traveling-salesman-using-dynamic-programming-b ...
- ALGO-11_蓝桥杯_算法训练_瓷砖铺放(递归)
问题描述 有一长度为N(<=N<=)的地板,给定两种不同瓷砖:一种长度为1,另一种长度为2,数目不限.要将这个长度为N的地板铺满,一共有多少种不同的铺法? 例如,长度为4的地面一共有如下5 ...
- Lucene suggest [转]
The Big Data Zone is presented by Splunk, the maker of data analysis solutions such as Hunk, an an ...
- Mongodb条件查询Query的用法
Query.All("name", "a", "b");//通过多个元素来匹配数组Query.And(Query.EQ("name ...
- Scala传名参数(By-Name)
1.=> 在了解Scala的By-Name参数之前,先了解下“=>”在Scala中有几种用法: 1.对于值,“=>”相当于lambda表达式.如: scala> List(,, ...
- 客户端负载均衡Feign之二:Feign 功能介绍
一.Ribboon配置 在Spring cloud Feign中客户端负载均衡是通过Spring cloud Ribbon实现的,所以我们可以直接通过配置Ribbon客户端的方式来自定义各个服务客户端 ...
- PHP中常用的数组函数总结
整理了一份PHP开发中数组操作大全,包含有数组操作的基本函数,数组的分段和填充,数组与栈,数组与列队,回调函数,排序,计算,其他的数组函数等. 一,数组操作的基本函数 数组的键名和值 array_va ...