ASP.NET MVC file download sample
ylbtech- ASP.NET MVC:ASP.NET MVC file download sample |
功能描述:ASP.NET MVC file download sample
2,TechnologyAndTheEnvironment(技术与环境) |
操作系统: |
windows |
开发语言: |
C# |
开发框架: |
ASP.NET MVC |
数据库: |
|
开发软件: |
Microsoft Visual Studio 2010 |
||
开发技术 |
ASP.NET MVC |
3,DatabaseDesign(数据库设计) |
4,FeatureScreenshots(功能截图) |
4.App_Data
/App_Data/download/testcert2.cer
/App_Data/download/testdata.zip
/App_Data/download/testfile.txt
/App_Data/download/XMLFile2.xml
4.Models
4.Views
/Views/File/List.aspx
<h2>
All files available for download:</h2>
<table style="width: 100%">
<thead>
<td>
FileName
</td>
<td>
Size(bytes)
</td>
<td style="width: 40">
</td>
</thead>
<%
var fileList = (List<System.IO.FileInfo>)Model;
foreach (var file in fileList)
{
%>
<tr>
<td>
<%= Html.ActionLink(file.Name,"Download",new {Action="Download", fn=file}) %>
</td>
<td>
<%=file.Length %>
</td>
<td>
<a href='<%= ResolveUrl("~/File/Download/"+ file.Name) %>'>
<img width="30" height="30" src='<%= ResolveUrl("~/images/download-icon.gif") %>' />
</a>
</td>
</tr>
<%} %>
</table>
4.Controllers
/Controllers/FileController.cs
/****************************** Module Header ******************************\
Module Name: FileController.cs
Project: CSASPNETMVCFileDownload
Copyright (c) Microsoft Corporation. This module contains the FileController class. FileController is the controller dedicated for file downloading functionality.
For request to list file, FileController will call List Action to return the
file list and display it via File/List view File request to download a certain file, FileController will call the
Download action to return the stream of the requested file. This source is subject to the Microsoft Public License.
See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
All other rights reserved. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.IO; namespace CSASPNETMVCFileDownload.Controllers
{
public class FileController : Controller
{
// Action for list all the files in "~/App_Data/download" directory
public ActionResult List()
{
// Retrieve the file list.
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/App_Data/download/")); // Filter it via LINQ to Object.
var files = from f in dir.GetFiles("*.*", SearchOption.TopDirectoryOnly)
where f.Extension != "exe"
select f; // Call the corresponding View.
return View(files.ToList());
} // Action for returning the binary stream of a specified file.
public ActionResult Download(string fn)
{
// Check whether the requested file is valid.
string pfn = Server.MapPath("~/App_Data/download/" + fn);
if (!System.IO.File.Exists(pfn))
{
throw new ArgumentException("Invalid file name or file not exists!");
} // Use BinaryContentResult to encapsulate the file content and return it.
return new BinaryContentResult()
{
FileName = fn,
ContentType = "application/octet-stream",
Content = System.IO.File.ReadAllBytes(pfn)
};
}
}
}
6,Sample|Explain FreeDownload(示例|讲解案例下载) |
![]() |
作者:ylbtech 出处:http://ylbtech.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
ASP.NET MVC file download sample的更多相关文章
- [转]ASP.NET MVC 2: Model Validation
本文转自:http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx?CommentPo ...
- asp.net mvc return file result
asp.net mvc返回文件: public ActionResult ExportReflection(string accessToken) { var reflections = GetCms ...
- 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 ...
- Cordova+Asp.net Mvc+GIS跨平台移动应用开发实战1-系统初步搭建(附演示,apk,全部源码)
1.前言 身处在移动互联网的今天,移动应用开发炙手可热,身为程序猿的我们怎么能错过开发一款我们自己的APP.本人算是一个基于.net的GIS开发入门者(马上就大四啦), 暑假在学校参加GIS比赛有大把 ...
- CRUD Operations In ASP.NET MVC 5 Using ADO.NET
Background After awesome response of an published by me in the year 2013: Insert, Update, Delete In ...
- Professional C# 6 and .NET Core 1.0 - Chapter 41 ASP.NET MVC
What's In This Chapter? Features of ASP.NET MVC 6 Routing Creating Controllers Creating Views Valida ...
- [转]Creating an Entity Framework Data Model for an ASP.NET MVC Application (1 of 10)
本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/creating-a ...
- Areas in ASP.NET MVC 4
Download source - 2.7 MB Introduction to Areas In this article, we will learn the concept of Areas a ...
- [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)
本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/sorting-fi ...
随机推荐
- NYOJ 211 Cow Contest (弗洛伊德+传递闭包 )
title: Cow Contest 弗洛伊德+传递闭包 nyoj211 tags: [弗洛伊德,传递闭包] 题目链接 描述 N (1 ≤ N ≤ 100) cows, conveniently nu ...
- WebSocket最简易理解,term.js插件的使用
介绍WebSocket WebSocket我想大家并不陌生,无论你的的工作中是否用到了它,都或多或少听说过,它是h5中新增的,在某些方面可以很好的替代我们一直沿用的ajax技术,甚至更加的出色.但是它 ...
- alt+ F8 设置无效(转)
原文转自 https://blog.csdn.net/m372897500/article/details/7310251 具体修改方法如下: 工具-选项-环境-键盘-应用以下其他键盘映射方案,选择v ...
- Python阶段复习 - part 2 - Python序列/持久化
1. 把一个数字的list从小到大排序,然后写入文件,然后从文件中读取出来文件内容,然后反序,在追加到文件的下一行中 >>> import json >>> imp ...
- 上传代码到github上
初始化 git init 添加远程仓库 git remote add origin[仓库名] 仓库地址 添加文件 git add . 本地提交 git commit -m 'message' 拉去远程 ...
- 【MT8382/8121】使用绝对路径编译模块会导致recourse_overlay无法应用的问题
之前为了方便mm模块编译,写了个脚本,实现了在任意模块其子目录下执行脚本即可编译的功能. 其实原理就是一层一层目录地往上寻找Android.mk文件,找到存放Android.mk目录后,就把该目录当作 ...
- windows下nginx安装及使用
nginx简介: nginx是一款轻量级web服务器,也是一款反向代理服务器(比如域名转发等). nginx功能: 1.可直接支持Rails和PHP的程序. 2.可作为HTTP反向代理服务器. 3.作 ...
- hdu 1423(LCS+LIS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1423 好坑啊..还有公共串为0时的特殊判断,还有格式错误..看Discuss看知道除了最后一组测试数据 ...
- shipyard, swarm看到你,我才睡觉:)
windows下用boot2docker有点不爽,就是网卡识别不好. 还是用纯的virtualbox建centos7虚拟机来搞, 一下就OK. 但记得,要用Iptables来弄墙,去掉firewall ...
- 变量与指针 --- WalMart 寄存箱设计者不懂计算机
一.场景: 今天星期天,难得公司售后同事很给力,项目运行正常,无运维事务需要处理.于是满足堂客(湖南人称老婆)很久来的心愿 陪其逛街(这里要惭愧 检讨作为IT码农常常容易忽略身边的人.大声疾呼:“码农 ...