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 ...
随机推荐
- Jmeter4.0启动闪退问题解决方案
jmeter:4.0 jdk版本:1.8 在Jmeter.bat的最后添加pause可以让Jmeter启动停止: 添加了pause进行强制停止在启动命令页面,查看到Jmeter报错信息如下: 第一次解 ...
- windows使用celery遇到的错误
https://www.jianshu.com/p/e5539d96641c 按照这个教程一步步执行到最后报错了. 运行task_dispatcher.py的时候 ValueError: not en ...
- PL/SQL 08 异常 exception
--PL/SQL错误 编译时 运行时 --运行时的出错处理 EXCEPTION --异常处理块DECLARE …BEGIN …EXCEPTION WHEN OTHERS THEN handle ...
- css3带你实现酷炫效果
css3 私有前缀 -webkit- chrome/safari等webkit内核浏览器 -moz- firfox -o- opera -ms- IE css3 盒子模型 box-sizing 值co ...
- 2.aiomysql实现对数据库异步读取
有一个库叫做aiomysql,这是一个基于asyncio和pymysql的库.至于为什么可以在tornado中使用,是因为高版本tornado的底层使用了asyncio. import asyncio ...
- ETL(Extract-Transform-Load的缩写,即数据抽取、转换、装载的过程)
ETL(Extract-Transform-Load的缩写,即数据抽取.转换.装载的过程)
- KVM(七)使用 libvirt 做 QEMU/KVM 快照和 Nova 实例的快照
本文将梳理 QEMU/KVM 快照相关的知识,以及在 OpenStack Nova 中使用 libvirt 来对 QEMU/KVM 虚机做快照的过程. 1. QEMU/KVM 快照 1.1 概念 QE ...
- ORM中的N+1问题
在orm中有一个经典的问题,那就是N+1问题,比如hibernate就有这个问题,这一般都是不可避免的. [N+1问题是怎么出现的] N+1一般出现在一对多查询中,下面以Group和User为例,Gr ...
- 只用120行Java代码写一个自己的区块链-3挖矿算法
在本系列前两篇文章中,我们向大家展示了如何通过精炼的Java代码实现一个简单的区块链.包括生成块,验证块数据,广播通信等等,这一篇让我们聚焦在如何实现 PoW算法. 大家都无不惊呼比特币.以太坊及其他 ...
- 【xunsearch】笔记
1.添加索引 $ cd /usr/local/xunsearch/sdk/php/ $ util/Indexer.php --rebuild --source=mysql://数据库用户名:数据库密码 ...
