【转】 .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的文件,竟然只用了几秒钟,从带宽上限制可以得出,实际上传文件是不可能的 ...
随机推荐
- Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(
application.class要放在根目录下,否则会发生以下错误
- centos中nodejs npm安装cordova
1.安装cordova npm install -g cordova --registry https://registry.npm.taobao.orgnpm install -g ionic -- ...
- php结合phantomjs实现网页截屏、抓取js渲染的页面
首先PhantomJS快速入门 PhantomJS是一个基于 WebKit 的服务器端 JavaScript API.它全面支持web而不需浏览器支持,其快速,原生支持各种Web标准: DOM 处理, ...
- ML平台_小米深度学习平台的架构与实践
(转载:http://www.36dsj.com/archives/85383)机器学习与人工智能,相信大家已经耳熟能详,随着大规模标记数据的积累.神经网络算法的成熟以及高性能通用GPU的推广,深度学 ...
- 基于selector的socket并发
server: #!_*_coding:utf-8_*_ #__author__:"Alex huang" import selectors #selector模块集成了selec ...
- Java ArrayList排序方法详解
由于其功能性和灵活性,ArrayList是 Java 集合框架中使用最为普遍的集合类之一.ArrayList 是一种 List 实现,它的内部用一个动态数组来存储元素,因此 ArrayList 能够在 ...
- 黄聪:安装cnpm替代默认的npm
使用cnpm替代默认的npm: npm install -g cnpm --registry=https://registry.npm.taobao.org
- 【linux】centos6.9设置etc0网卡开机自动获取ip
在vm新安装的centos系统中,一般选择NAT来设置和主机共享局域网,通过ifconfig etc0 192.168.xx.xx 这种作法机器重启之后就会失效,所以可以使用更改文件的方式完成设置ce ...
- LeetCode——12. Integer to Roman
一.题目链接:https://leetcode.com/problems/integer-to-roman/ 二.题目大意: 给定一个整数,返回它的罗马数字的形式. 三.题解: 要想做出这道题目,首先 ...
- 前端模拟后台返回数据之Mockjs
一.官方文档: https://github.com/nuysoft/Mock/wiki/Syntax-Specification 例子:http://mockjs.com/examples.html ...