上传文件是一项基本功能,一定要了解的。先来看一下使用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. I.MX6 Linux、Jni ioctl 差异

    /*********************************************************************** * I.MX6 Linux.Jni ioctl 差异 ...

  2. HDU 5038 Grade

    解题思路:这题最关键的是要读懂题意,If not all the value are the same but the frequencies of them are the same, there ...

  3. 【英语】Bingo口语笔记(60) - 口语中的浊化发音

  4. android adb应用

    一 adb 简介 ADB是一个 客户端-服务器端 程序, 其中客户端是你用来操作的电脑, 服务器端是android设备. 二 安装 方法 先说安装方法, 电脑上需要安装客户端. 客户端包含在sdk里. ...

  5. android中的所谓观察者模式

    生活中我们常认定某些人很有才,但什么是有才呢?明朝的王守仁曾这样解释:才,是所谓天理,应用到物上,便成了才.凡事凡物,只要掌握了所谓科学的方法,并能灵活运用,那么你也可以成为一个有才的人. 观察者模式 ...

  6. unity, sprite atlas

    一, Sprite Packer 可以直接在unity里放碎图,只要将Texture Type选为Sprite(2D and UI),Sprite Mode选为Single,再把想打在一张大图里的碎图 ...

  7. ios import和@class的区别

    二者的区别在于: 1.import会包含这个类的所有信息,包括实体变量和方法,而@class只是告诉编译器,其后面声明的名称是类的名称,至于这些类是如何定义的,暂时不用考虑,后面会再告诉你. 2.在头 ...

  8. distinguish and differentiate

    According to Cambridge Dictionary distinguish:to recognize or understand the difference between two ...

  9. 身份证上的X到底代表什么?

    生活中,无论你是坐火车,还是办理各种手续,都需要用到身份证,它现在已经俨然成为我们生活的非常重要的一部分,但是关于身份证本身,你了解多少呢? 有人会说了,为什么我的身份证上写的有效时间是10年,而一些 ...

  10. JdbcTemplate与事务

    JdbcTemplate操作采用的是JDBC默认的AutoCommit模式,也就是说我们还无法保证数据操作的原子性(要么全部生效,要么全部无效),如: JdbcTemplate jdbcTemplat ...