上传文件是一项基本功能,一定要了解的。先来看一下使用ASP.NET MVC实现简单的上传。

一、简单的例子

  Controller的例子

        public ActionResult UploadDemo()
{
return View();
} public ActionResult FileUpload()
{
HttpPostedFileBase file = Request.Files["file"];
if (file != null)
{
string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(file.FileName));
file.SaveAs(filePath); Response.Write("<script>alert('上传成功');location.href='/Index/UploadDemo' </script>");
}
else
{
Response.Write("<script>alert('上传失败');location.href='/Index/UploadDemo' </script>");
} return null;
}

  对应的View视图

@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>UploadDemo</title>
</head>
<body>
<div>
<h2>
上传例子</h2>
@using (Html.BeginForm("FileUpload", "Index", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type="submit" value="uploadFile" />
}
</div>
</body>
</html>

ASP.NET MVC- Upload File的例子的更多相关文章

  1. ASP.Net MVC upload file with record & validation - Step 6

    Uploading file with other model data, validate model & file before uploading by using DataAnnota ...

  2. Asp.net mvc 3 file uploads using the fileapi

    Asp.net mvc 3 file uploads using the fileapi I was recently given the task of adding upload progress ...

  3. asp.net mvc return file result

    asp.net mvc返回文件: public ActionResult ExportReflection(string accessToken) { var reflections = GetCms ...

  4. asp.net mvc使用jwt简单例子

    Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准.该token被设计为紧凑且安全的,特别适用于分布式站点的单点登录(SSO)场景.JWT的声 ...

  5. Asp.net MVC 控制器ActionResult的例子

    ActionResult 父类型 ViewResult View() 多重载应用 PartialViewResult PartialView() 部分试图 New EmptyResult()  空 如 ...

  6. asp.net mvc 5 单元测试小例子

    using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTest ...

  7. ASP.NET MVC 例子演示如何在 Knockout JS 的配合下,使用 TypeScript 。

    一个简单的 ASP.NET MVC 例子演示如何在 Knockout JS 的配合下,使用 TypeScript . 前言 TypeScript 是一种由微软开发的自由和开源的编程语言.它是JavaS ...

  8. 【转】ASP.NET MVC框架下使用MVVM模式-KnockOutJS+JQ模板例子

    KnockOutJS学习系列----(一) 好几个月没去写博客了,最近也是因为项目紧张,不过这个不是借口,J. 很多时候可能是因为事情一多,然后没法静下来心来去写点东西,学点东西. 也很抱歉,突然看到 ...

  9. ASP.NET MVC file download sample

    ylbtech- ASP.NET MVC:ASP.NET MVC file download sample 功能描述:ASP.NET MVC file download sample 2,Techno ...

随机推荐

  1. web前端调试工具

    1.firebug入门指南 http://www.ruanyifeng.com/blog/2008/06/firebug_tutorial.html 2. Console命令详解,让调试js代码变得更 ...

  2. ecshop 改变sitemap.xml的位置

    大家知道ECSHOP默认的sitemap.xml文件是放置在data文件夹中的,但是这不利于GOOGLE的抓取.我们必须把sitemap.xml文件放置在根目录下 在admin/sitemap.php ...

  3. Maven加依赖包

    对于初学maven的人来说刚开始会有个困惑,那就是怎么知道依赖的jar的groupId和atrifactId是什么, 比如要依赖mybatis,会在pom.xml中配置如下: <dependen ...

  4. Arduino报错

    avrdude: stk500_recv(): programmer is not respondingavrdude: stk500_getsync() attempt 1 of 10: not i ...

  5. android性能小贴士 翻译

    转自http://developer.android.com/training/articles/perf-tips.html 性能小贴士: 这篇文档主要一些微优化可以提升应用程序性能,但是这些改变不 ...

  6. pthread_attr_t 线程属性(一)

    1.    线程属性:             使用pthread_attr_t类型表示,我们需要对此结构体进行初始化,                 初始化后使用,使用后还要进行去除初始化!    ...

  7. Java魔法类:sun.misc.Unsafe

    Unsafe类在jdk 源码的多个类中用到,这个类的提供了一些绕开JVM的更底层功能,基于它的实现可以提高效率.但是,它是一把双刃剑:正如它的名字所预示的那样,它是Unsafe的,它所分配的内存需要手 ...

  8. angularjs $swipe调用方法

    angularjs 的$swipe,用法: $swipe.bind(angular.element(document),{ start: function(pos) { }, move: functi ...

  9. bzoj3620 似乎在梦中见过的样子

    好久没有写过KMP了,今天写个KMP练练手.此题就是枚举左端点暴力,用KMP做到O(n^2) #include<cstdio> #include<cstring> using ...

  10. C语言练习:第二大整数

    问题描述 编写一个程序,读入一组整数(不超过20个),当用户输入0时,表示输入结束.然后程序将从这组整数中,把第二大的那个整数找出来,并把它打印出来.说明:(1)0表示输入结束,它本身并不计入这组整数 ...