上传和下载

在这里插入图片描述

1、//DAL层FileDAL类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;

namespace DAL
{
public class FileDAL
{
///
/// 上传文件
///
/// 文件模型
///
public int OnLoad(FileInfo mod)
{
string sql = $“insert into FileInfo values(’{mod.FName}’,{mod.FSize},’{mod.FileUrl}’)”;
return DBHelper.ExecuteNonQuery(sql);
}

    /// <summary>
    /// 显示列表
    /// </summary>
    /// <returns></returns>
    public List<FileInfo> GetFileInfos()
    {
        string sql = "select * from FileInfo";
        return DBHelper.GetList<FileInfo>(sql);
    }
}

}

2、控制器

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DAL;
using Model;

namespace WebUpFileMVC.Controllers
{
public class FileController : Controller
{
FileDAL dal = new FileDAL();

    // GET: File

    /// <summary>
    /// 列表界面
    /// </summary>
    /// <returns></returns>
    public ActionResult Main()
    {
        var list = dal.GetFileInfos();
        return View(list);
    }

    /// <summary>
    /// 实现文件上传功能
    /// </summary>
    /// <param name="file">上传的文件</param>
    /// <returns></returns>
    [HttpPost]
    public int UpLoad(HttpPostedFileBase file)
    {
        if (file==null)
        {
            return -1;
        }
        //获取文件名
        var fileName = file.FileName;
        //获取文件物理路径
        var filePath = Server.MapPath("/Files");
        var fName = Path.Combine(filePath, fileName);
        //保存文件
        file.SaveAs(fName);
        var info = new Model.FileInfo()
        {
            FName = fileName,
            FSize = file.ContentLength,
            FileUrl = "/Files/" + fileName
        };
        return dal.OnLoad(info);
    }

    /// <summary>
    /// 实现文件下载功能
    /// </summary>
    /// <param name="fileName">文件名</param>
    /// <returns></returns>
    public ActionResult DownLoad(string fileName)
    {
        //获取文件物理路径
        var filePath = Server.MapPath("~/Files");
        var fName = Path.Combine(filePath, fileName);
        return File(fName, "application/*", fileName);
    }
}

}

3、前台页面

@{
Layout = null;
}
@using Model
@model List<Model.FileInfo>

<meta name="viewport" content="width=device-width" />
<title>Main</title>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<script src="~/Content/jquery-3.3.1.js"></script>
<script src="~/Content/jquery.unobtrusive-ajax.min.js"></script>


<div style="width:800px;margin:auto;margin-top:50px"> 
    @using (Ajax.BeginForm("UpLoad",null,new AjaxOptions { HttpMethod="post",OnSuccess="upLoadSuccess" },new { enctype = "multipart/form-data" }))
    {
        <table>
            <tr>
                <td><input id="fileName" type="file" name="file" /></td>
                <td>
                    <input id="Submit1" type="submit" value="上传文件" class="btn btn-default" />
                </td>
            </tr>
        </table>
    }
<table class="table table-striped">
    <thead>
        <tr>
            <th>编号</th>
            <th>文件名</th>
            <th>文件大小</th>
            <th>下载</th>
        </tr>
    </thead>
    @*正确显示已经上传文件的信息*@
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>@item.FId</td>
                <td>@item.FName</td>
                <td>@(item.FSize)kb</td>
                <td><a href="javascript:downLoad('@item.FName')">下载</a></td>
            </tr>
        }
    </tbody>
</table>
</div>



//成功回调函数
function upLoadSuccess(n) {
    if (n == -1) {
        alert("请选择上传的文件!");
    }
    else if (n>0) {
        alert("上传成功!");
        location.reload();
    }
    else {
        alert("上传失败!");
    }
}

function downLoad(fileName) {
    location.href = "/File/DownLoad?fileName=" + fileName;
}

在这里插入图片描述

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页