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

  1. <h2>
  2. All files available for download:</h2>
  3. <table style="width: 100%">
  4. <thead>
  5. <td>
  6. FileName
  7. </td>
  8. <td>
  9. Size(bytes)
  10. </td>
  11. <td style="width: 40">
  12. </td>
  13. </thead>
  14. <%
  15. var fileList = (List<System.IO.FileInfo>)Model;
  16. foreach (var file in fileList)
  17. {
  18. %>
  19. <tr>
  20. <td>
  21. <%= Html.ActionLink(file.Name,"Download",new {Action="Download", fn=file}) %>
  22. </td>
  23. <td>
  24. <%=file.Length %>
  25. </td>
  26. <td>
  27. <a href='<%= ResolveUrl("~/File/Download/"+ file.Name) %>'>
  28. <img width="30" height="30" src='<%= ResolveUrl("~/images/download-icon.gif") %>' />
  29. </a>
  30. </td>
  31. </tr>
  32. <%} %>
  33. </table>

4.Controllers

/Controllers/FileController.cs

  1. /****************************** Module Header ******************************\
  2. Module Name: FileController.cs
  3. Project: CSASPNETMVCFileDownload
  4. Copyright (c) Microsoft Corporation.
  5.  
  6. This module contains the FileController class.
  7.  
  8. FileController is the controller dedicated for file downloading functionality.
  9. For request to list file, FileController will call List Action to return the
  10. file list and display it via File/List view
  11.  
  12. File request to download a certain file, FileController will call the
  13. Download action to return the stream of the requested file.
  14.  
  15. This source is subject to the Microsoft Public License.
  16. See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  17. All other rights reserved.
  18.  
  19. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  20. EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  22. \***************************************************************************/
  23.  
  24. using System;
  25. using System.Collections.Generic;
  26. using System.Linq;
  27. using System.Web;
  28. using System.Web.Mvc;
  29. using System.Web.Mvc.Ajax;
  30. using System.IO;
  31.  
  32. namespace CSASPNETMVCFileDownload.Controllers
  33. {
  34. public class FileController : Controller
  35. {
  36. // Action for list all the files in "~/App_Data/download" directory
  37. public ActionResult List()
  38. {
  39. // Retrieve the file list.
  40. DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/App_Data/download/"));
  41.  
  42. // Filter it via LINQ to Object.
  43. var files = from f in dir.GetFiles("*.*", SearchOption.TopDirectoryOnly)
  44. where f.Extension != "exe"
  45. select f;
  46.  
  47. // Call the corresponding View.
  48. return View(files.ToList());
  49. }
  50.  
  51. // Action for returning the binary stream of a specified file.
  52. public ActionResult Download(string fn)
  53. {
  54. // Check whether the requested file is valid.
  55. string pfn = Server.MapPath("~/App_Data/download/" + fn);
  56. if (!System.IO.File.Exists(pfn))
  57. {
  58. throw new ArgumentException("Invalid file name or file not exists!");
  59. }
  60.  
  61. // Use BinaryContentResult to encapsulate the file content and return it.
  62. return new BinaryContentResult()
  63. {
  64. FileName = fn,
  65. ContentType = "application/octet-stream",
  66. Content = System.IO.File.ReadAllBytes(pfn)
  67. };
  68. }
  69. }
  70. }
6,Sample|Explain FreeDownload(示例|讲解案例下载)
作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

ASP.NET MVC file download sample的更多相关文章

  1. [转]ASP.NET MVC 2: Model Validation

    本文转自:http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx?CommentPo ...

  2. asp.net mvc return file result

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

  3. 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 ...

  4. Cordova+Asp.net Mvc+GIS跨平台移动应用开发实战1-系统初步搭建(附演示,apk,全部源码)

    1.前言 身处在移动互联网的今天,移动应用开发炙手可热,身为程序猿的我们怎么能错过开发一款我们自己的APP.本人算是一个基于.net的GIS开发入门者(马上就大四啦), 暑假在学校参加GIS比赛有大把 ...

  5. 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 ...

  6. 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 ...

  7. [转]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 ...

  8. 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 ...

  9. [转]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 ...

随机推荐

  1. BroadCastRecieve

    首先介绍一下BroadCastRecieve有几种: 1.无序广播(普通广播):sendBroadcast()方式 2.有序广播:sendOrderedBroadcast()方式 3.粘性广播:sen ...

  2. C++中 相对路径与绝对路径 斜杠 '/' 与反斜杠 '\'的区别

    文件路径正斜杠和反斜杠 正斜杠,又称左斜杠,符号是"/":反斜杠,也称右斜杠,符号是"\".文件路径的表示可以分为绝对路径和相对路径: 1.绝对路径表示相对容易 ...

  3. linux中的阻塞机制及等待队列【转】

    转自:http://www.cnblogs.com/gdk-0078/p/5172941.html 阻塞与非阻塞是设备访问的两种方式.驱动程序需要提供阻塞(等待队列,中断)和非阻塞方式(轮询,异步通知 ...

  4. kafka 分区数

    Kafka的分区,相当于把一个Topic再细分成了多个通道(对应 多个线程) 部署的时候尽量做到一个消费者(线程)对应一个分区. 如何确定Kafka的分区数,key和consumer线程数,以及不消费 ...

  5. Selenium2+python自动化69-PhantomJS使用【转载】

    前言 PhantomJS是一个没有界面的浏览器,本质上是它其实也就是一个浏览器,只是不在界面上展示. PhantomJS非常适合爬虫方面,很多玩爬虫的都喜欢用这个浏览器. 一.PhantomJS环境准 ...

  6. Selenium2+python自动化48-登录方法(参数化)【转载】

    前言 登录这个场景在写用例的时候经常会有,我们可以把登录封装成一个方法,然后把账号和密码参数化,这样以后用的登录的时候,只需调用这个方法就行了 一.登录方法 1.把输入账号.输入密码.点击登录按钮三个 ...

  7. quote(),unquote(),urlencode()编码解码

    quote(),unquote(),quote_plus(),unquote_plus(),urlencode() ,pathname2url(),url2pathname() urllib中还提供了 ...

  8. 解决nextjs部署到now上之后出现的“Unable to import module 'now__launcher'”错误

    解决nextjs部署到now上之后出现的“Unable to import module 'now__launcher'”错误 这个错误是由于在next.config.js中直接引用了withLess ...

  9. python+selenium 组织用例方式 总结

    1.unittest.main() 将一个单元测试模块变为可直接运行的测试脚本,main()方法使用TestLoader类来搜索所有包含在该模块中以“test”命名开头的测试方法,并自动执行他们.执行 ...

  10. easyui中导航菜单accordion与tree的动态添加

    博客分类: Java Web开发   Js代码   $.parser.parse(); $.ajax({ url:my.bp()+'/main/menuaction!createMenu.action ...