zoukankan      html  css  js  c++  java
  • 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=" 100%">
            <thead>
                <td>
                    FileName
                </td>
                <td>
                    Size(bytes)
                </td>
                <td style=" 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/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    星云精准测试有力提升金融复杂系统的测试能效
    疫情之下,精准测试的智能可信模式正在成为中流砥柱
    星云测试插装编译流程与CI集成
    自动化测试与精准测试的无缝对接
    “静默式”精准测试,让企业零成本完成黑盒测试的升级对接
    精准测试与开源工具Jacoco的覆盖率能力大PK
    【星云测试】Devops微服务架构下具有代码级穿透能力的精准测试
    【星云测试】开发者测试-采用精准测试工具对Spring Boot应用进行测试
    分享我们团队管理的最佳实践——程序员的周报应如何填写
    [原创]基于VueJs的前后端分离框架搭建之完全攻略
  • 原文地址:https://www.cnblogs.com/ylbtech/p/4189148.html
Copyright © 2011-2022 走看看