zoukankan      html  css  js  c++  java
  • 简单二进制上传下载文件

    前端代码:

    @model Business.Models.Model
    @{
        ViewBag.Title = "上传文件";
    }
    
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script type="text/javascript">
        var Input = {};
        Input.Save = function (e) {
            if ($("#id_file").val() == "") {
                alert("请选择文件!");
                return;
            }
     
            var frm = $(e).parents("form");
            $(".tabs-panels .panel:visible").find('#frmInput').submit();//frmInput为FormID
        };
     
        Input.SaveCallBack = function (result, desc) {
            if (result) {
                //刷新列表
                $('#tbList').datagrid('reload');
                alert("提交成功!");
                //关闭选项卡
                Common.CloseTab();
            }
            else {
                alert("保存失败,请重试!");
            }
        };
    </script>
    @using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { enctype = "multipart/form-data", @id = "frmInput" }))
    {
        <h6 class="tg-left tt-block title tgd-l">
            文件信息</h6>
        <div style="margin-top:50px;">
            <table class="tbl-input" cellspacing="0">
                <colgroup>
                    <col style=" 400px;" />
                    <col class="w5" style=" 400px;" />
                    <col style=" 150px;" />
                </colgroup>
                <tbody>
                    <tr>
                        <th scope="row">
                            @Html.LabelFor(model => model.ShotName):
                        </th>
                        <td>
                            @Html.TextBoxFor(model => model.ShotName, new { style = "300px;", @id = "id_ShotName" })
                        </td>
                        <td>
                            <span class=" btn-infomust">必填项</span>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">
                            文件:
                        </th>
                        <td>
                            <div id="files">
                                <input type="file" name="FileUpload1" id = "id_file"/><br />
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <p class="op" style="text-align: center; margin-top: 50px; margin-bottom: 30px;">
            <input type="button" id="btnSave" onclick="Input.Save(this);" class="btn-8" value="保存"/>
            <input type="button" value="返回" onclick="Common.CloseTab(this);" class="btn-8" />
        </p>
    }

    后台:

    namespace Ports.Controllers.Ports.Controllers
    {
        [Description("附件管理")]
        public class AttachmentController : BaseController
        {
    #region 文件上传
            /// <summary>
            /// 文件上传
            /// </summary>
            /// <returns></returns>
            /// 
            public ActionResult Create()
            {
                var model = new AttachmentModel { };
                return View("Create", model);
            }
     
            [HttpPost]
            public ActionResult Upload(AttachmentModel model)
            {
                //使用此控件会限制文件大小最大上传4m
                //可在web.config中配置<httpRuntime executionTimeout= "5400" maxRequestLength= "2048000"
                // useFullyQualifiedRedirectUrl= "false " /> , 自定义上传大小
           //详细见http://www.cnblogs.com/xcsn/archive/2012/12/26/2833442.html
     
                foreach (string upload in Request.Files)
                {
                    if (!HasFiles.HasFile(Request.Files[upload])) continue;
     
                    string miniType = Request.Files[upload].ContentType;
                    string filename = Path.GetFileName(Request.Files[upload].FileName);
                    //转换文件流成字节数组
                    BinaryReader reader = new BinaryReader(Request.Files[upload].InputStream);
                    byte[] fileData = reader.ReadBytes((int)reader.BaseStream.Length);
                    model.ShotName = model.ShotName == null ? "默认名称" : model.ShotName;
                    model.Author = "";
              //插入
                    AttachmentRule.Instance.Insert(model.ShotName, filename, miniType, fileData, model.Author, "");
                }
                return View("Index");
            }
            #endregion
    
            #region 文件下载
            /// <summary>
            /// 文件下载
            /// </summary>
            /// <param name="id">附件唯一标识</param>
            /// <returns></returns>
            public FileContentResult GetFile(int id)
            {//前台直接使用链接,点击自动下载
                var fileinfo = AttachmentRule.Instance.GetEntity(id);
                return File(fileinfo.FileData, fileinfo.MimeType, fileinfo.FileName);
            }
            #endregion
      }
    }
    public static class HasFiles
    {
        public static bool HasFile(this HttpPostedFileBase file)
        {
            return (file != null && file.ContentLength > 0) ? true : false;
        }
    }
  • 相关阅读:
    [模板]KMP
    [BZOJ] 1833: [ZJOI2010]count 数字计数
    [BZOJ] 1563: [NOI2009]诗人小G
    [BZOJ] 2442: [Usaco2011 Open]修剪草坪
    [LOJ] #2360. 「NOIP2016」换教室
    9.18模拟赛
    [BZOJ] 2006: [NOI2010]超级钢琴
    [BZOJ] 1143: [CTSC2008]祭祀river
    [51Nod] 1218 最长递增子序列 V2
    [BZOJ] 3307: 雨天的尾巴
  • 原文地址:https://www.cnblogs.com/xcsn/p/2843145.html
Copyright © 2011-2022 走看看