当前位置: 首页 > news >正文

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>
View Code

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)
            };
        }
    }
}
View Code

 

6,Sample|Explain FreeDownload(示例|讲解案例下载)

 

warn作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载于:https://www.cnblogs.com/ylbtech/p/4189148.html

相关文章:

  • 【转】foxmail邮箱我已进清理了为什么还是说我的邮箱已满
  • 【Shell脚本学习8】Shell特殊变量:Shell $0, $#, $*, $@, $?, $$和命令行参数
  • Linux下使用popen()执行shell命令
  • 便利记事本发布说明
  • session的取代者:Json Web Tokens----在客户端存储登陆状态
  • Web安全测试指南--权限管理
  • C#阶段小结
  • gnome3桌面
  • erlang浅谈
  • 安卓下拉刷新
  • Android点击EditText文本框之外任何地方隐藏键盘的解决办法
  • Jquery 调用.net WebService 返回Json、XML方法
  • Silverlight 之 创建
  • hdu 5122 K.Bro Sorting
  • ios编译库文件时出现的问题
  • CSS3 变换
  • CSS3 聊天气泡框以及 inherit、currentColor 关键字
  • ES6, React, Redux, Webpack写的一个爬 GitHub 的网页
  • FineReport中如何实现自动滚屏效果
  • Python socket服务器端、客户端传送信息
  • SQLServer插入数据
  • Three.js 再探 - 写一个跳一跳极简版游戏
  • Zsh 开发指南(第十四篇 文件读写)
  • 基于web的全景—— Pannellum小试
  • 什么软件可以剪辑音乐?
  • 实现简单的正则表达式引擎
  • 使用putty远程连接linux
  • 算法-插入排序
  • 在Docker Swarm上部署Apache Storm:第1部分
  • Redis4.x新特性 -- 萌萌的MEMORY DOCTOR
  • 阿里云API、SDK和CLI应用实践方案
  • 继 XDL 之后,阿里妈妈开源大规模分布式图表征学习框架 Euler ...
  • ​如何防止网络攻击?
  • #pragma data_seg 共享数据区(转)
  • (一) springboot详细介绍
  • (转)linux 命令大全
  • (转)可以带来幸福的一本书
  • (转)利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载 【反射】...
  • (轉貼) VS2005 快捷键 (初級) (.NET) (Visual Studio)
  • ./configure,make,make install的作用(转)
  • .apk文件,IIS不支持下载解决
  • .MyFile@waifu.club.wis.mkp勒索病毒数据怎么处理|数据解密恢复
  • .net mvc部分视图
  • .NET 将多个程序集合并成单一程序集的 4+3 种方法
  • .NET 中创建支持集合初始化器的类型
  • .net 逐行读取大文本文件_如何使用 Java 灵活读取 Excel 内容 ?
  • .NET6 命令行启动及发布单个Exe文件
  • .NET6实现破解Modbus poll点表配置文件
  • @Autowired多个相同类型bean装配问题
  • @Resource和@Autowired的区别
  • [ 云计算 | AWS ] AI 编程助手新势力 Amazon CodeWhisperer:优势功能及实用技巧
  • [20180224]expdp query 写法问题.txt
  • [BROADCASTING]tensor的扩散机制
  • [BZOJ 4129]Haruna’s Breakfast(树上带修改莫队)
  • [CC2642R1][VSCODE+Embedded IDE+IAR Build+Cortex-Debug] TI CC2642R1基于VsCode的开发环境