zoukankan      html  css  js  c++  java
  • mvc上传,下载,浏览文件功能(用uploadify插件)

     public class UpLoadFileController : Controller
        {
            //
            // GET: /UpLoadFile/
    
            public ActionResult Index()
            {
                return View();
            }
    
            /// <summary>
            /// 上传文件【上传文件到UploadFiles文件夹】
            /// </summary>
            /// <param name="fileData"></param>
            /// <param name="Id"></param>
            /// <returns></returns>
            [AcceptVerbs(HttpVerbs.Post)]
            public ContentResult UpLoadFile(HttpPostedFileBase fileData, String Id)
            {
                ControllerContext.HttpContext.Request.ContentEncoding = Encoding.UTF8;
                ControllerContext.HttpContext.Response.ContentEncoding = Encoding.UTF8;
                ControllerContext.HttpContext.Response.Charset = "UTP-8";
    
                string filePath = Server.MapPath(@"/UploadFiles");
                if (!Directory.Exists(filePath))
                    Directory.CreateDirectory(filePath);
                if (fileData != null)
                {
                    string fileSaveName = Path.Combine(filePath, fileData.FileName);
                    fileData.SaveAs(fileSaveName);
                    return Content("成功");
                }
                return Content("失败");
            }
    
            /// <summary>
            /// 获取文件【下载】
            /// </summary>
            /// <param name="id">文件名</param>
            /// <returns></returns>
            public FilePathResult GetFile(string id)
            {
                if (string.IsNullOrEmpty(id))
                {
                    return null;
                }
                id = id.Replace("__", ".");
                string path = "UploadFiles";
                string filePath = Server.MapPath(@"/" + path);
                string fileSaveName = Path.Combine(filePath, id);
                return File(fileSaveName, "image/*", id);
            }
    
        }
    View Code

    Index.cshtml

    @{
        ViewBag.Title = "上传文件";
    }
    <script src="~/Content/uploadify/jquery.uploadify.js?ver=@DateTime.Now.Ticks"></script>
    <link href="~/Content/uploadify/uploadify.css" rel="stylesheet" />
    
    @*上传文件*@
    <div>
        <input class="easyui-validatebox" type="hidden" id="Attachment_GUID" name="Attachment_GUID" />
        <input id="file_upload" name="file_upload" type="file" >
       @* <a href="javascript:void(0)" class="easyui-linkbutton" id="btnUpload" data-options="plain:true,iconCls:'icon-save'"
            onclick="javascript: $('#file_upload').uploadify('upload', '*')">上传</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" id="btnCancelUpload" data-options="plain:true,iconCls:'icon-cancel'"
            onclick="javascript: $('#file_upload').uploadify('cancel', '*')">取消</a>
       <div id="fileQueue" class="fileQueue"></div>*@
         <a href="javascript:void(0)" class="easyui-linkbutton" id="btnBrowse" >浏览</a>
        <div id="div_files">
            <a id="downFile" href="#">下载文件</a>
            <img id="uploadImg" src=""/>  
        </div>
        <br />
    
        
        
    </div>
    
    
    @*弹出层*@
    @Html.Partial("Contact")
    
    <script>
    
        $(function () {
            $("#btnBrowse").click(function () {
                PopLayout.Pop($("#uploadImg").attr("src"));
            });
    
            $("#file_upload").uploadify({
                height: 30,
                 120,
                buttonText: "选择",
                multi: false,
                swf: '/Content/uploadify/uploadify.swf',
                uploader: '@Url.Action("UpLoadFile","UpLoadFile")',
                onFallback: function () {
                    alert("不支持flash");
                },
                onUploadError: function (file, errorCode, errorMsg, errorString) {
                    alert("文件" + file.name + "上传失败: " + errorString);
                },
                onUploadSuccess: function (file, data, response) {
                    if (true) {
                        //alert(file.name);
                        var id = file.name.replace(".", "__");;
                        $("#downFile").attr("href", "/UpLoadFile/GetFile/" + id);
                        $("#uploadImg").attr("src", "/UploadFiles/" + file.name);
                    }
                    //alert("文件" + file.name + "上传成功:" + data);
                }
            });
        });
    
    </script>
    View Code

    @Html.Partial("Contact")  嵌入Contact.cshtml文件

    请看上篇文章,弹出层:http://www.cnblogs.com/liujinwu-11/p/4334568.html  

  • 相关阅读:
    服务监控信息到底是“主动推送”还是“被动扫描”???
    spring boot metrics信息推送开发
    spring boot +RabbitMQ +InfluxDB+Grafara监控实践
    "敏捷革命"读书笔记
    对于搞技术的人怎样针对自己看什么书
    HBase数据库相关基本知识
    spring cloud 微服务日志跟踪 sleuth logback elk 整合
    日志收集(ElasticSearch)串联查询 MDC
    关于” 记一次logback传输日志到logstash根据自定义设置动态创建ElasticSearch索引” 这篇博客相关的优化采坑记录
    记一次logback传输日志到logstash根据自定义设置动态创建ElasticSearch索引
  • 原文地址:https://www.cnblogs.com/liujinwu-11/p/4334679.html
Copyright © 2011-2022 走看看