码迷,mamicode.com
首页 > Web开发 > 详细

mvc上传,下载,浏览文件功能(用uploadify插件)

时间:2015-03-13 12:16:31      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 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,
            width: 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  

mvc上传,下载,浏览文件功能(用uploadify插件)

标签:

原文地址:http://www.cnblogs.com/liujinwu-11/p/4334679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!