• 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  

  • 相关阅读:
    delphi 静态3维数组。 严重占用堆栈 切记。 应该用动态数组, 非要用静态数组的话, 要在编译器里 把 堆栈 调大
    Softmax 函数的特点和作用是什么?
    笔记本按开机键没反应怎么办
    小鸡 模拟器 支持双手柄。
    windows7 玩 WinKawaks kof2002为什么提示couldn't initialise DirectSound?
    Delphi中堆栈区别
    最让人纠结的等式:0.999...=1
    求 主板型号 945GME
    电脑可以识别sd卡手机无法识别 的解决方法。 我成功了。 淘宝买的sd卡 不用退货了。 退的人肝疼
    【线段树】HDU 5443 The Water Problem
  • 原文地址:https://www.cnblogs.com/liujinwu-11/p/4334679.html
Copyright © 2020-2023  润新知