您的位置:首页 > 产品设计 > UI/UE

C# MVC 使用EasyUIfileBox上传文件笔记

2017-04-26 09:30 323 查看
 <script type="text/javascript">

        //显示导入窗口

        function openF() {

            $('#import').window('open');

            document.getElementById("importFileForm").style.display = "block";

        }

        //关闭导入窗口

        function closeF() {

            document.getElementById('fileName').innerHTML = "";

            document.getElementById('uploadInfo').innerHTML = "";

            $('#import').window('close');

        }

        //导入文件

        function importF() {

            var file = document.getElementsByName("file1")[0].files[0];

            if (file == null) { alert('请选择文件'); return; }

            var fileName = file.name;

            var file_typename = fileName.substring(fileName.lastIndexOf('.'), fileName.length); //获取文件类型

            if (file_typename == '.xlsx' || file_typename == '.xls' || file_typename == '.doc' || file_typename == '.docx')//判断上传文件类型

            {

                var fileSize = 0;

                if (file.size > 1024 * 1024) {

                    fileSize = Math.round(file.size * 100 / (1024 * 1024)) / 100;

                    if (fileSize > 20) {

                        alert('文件不能超过20MB'); return;

                    }

                    fileSize = fileSize.toString() + 'MB';

                }

                else {

                    fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';

                }

                document.getElementById('fileName').innerHTML = "<span style='color:Blue'>大小:" + fileSize + "</span>";

                var formData = new FormData($("#importFileForm")[0]);

                $.ajax({

                    url: "/SiteInfo/PostExcelData",

                    type: 'post',

                    data: formData,

                    async: false,

                    cache: false,

                    contentType: false,

                    processData: false, //不显示进度条

                    success: function (returnInfo) {

                        document.getElementById('file1').value = null;

                        alert(returnInfo);

                    },

                    error: function (returnInfo) {

                        document.getElementById('uploadInfo').innerHTML = "<span style='color:Red'>" + returnInfo + "</span>";

                    }

                });

            }

            else {

                alert("文件类型错误");

                document.getElementById('fileName').innerHTML = "<span style='color:Red'>错误提示:上传文件应该是.xlsx|.xls|.doc|.docx后缀而不应该是" + file_typename + ",请重新选择文件</span>"

            }

        }
    </script>

 public string PostExcelData()

        {

            string info = string.Empty;

            try

            {

                

                HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;

                

                if (files.Count > 0)

                {

                    HttpPostedFile file = files[0];

                    string targetDir = System.Web.HttpContext.Current.Server.MapPath("../FileUpLoad/");

                    string path = System.IO.Path.Combine(targetDir, System.IO.Path.GetFileName(file.FileName));

                   

                    file.SaveAs(path);

                    info = "上传成功";

                }

                else

                {

                    info = "上传失败";

                }

            }

            catch

            {

                info = "上传失败";

            }

            return info;

        }

<div class="easyui-window" id="import" title="文件上传" style="width: 400px; height: 160px;

        padding: 2px;" closed="true">

        <form id="importFileForm" method="post" enctype="multipart/form-data">

        <table style="margin: 5px; height: 70px;">

            <tr>

                <td width="5px;">

                </td>

                <td>

                    <input class="easyui-filebox" name="file1" id="file1" style="width: 260px" />

                </td>

                <td>

                </td>

            </tr>

            <tr>

                <td colspan="4">

                    <label id="fileName" />

                </td>

            </tr>

            <tr>

                <td colspan="4">

                    <label id="uploadInfo" />

                </td>

            </tr>

        </table>

        <div style="text-align: center; clear: both; margin: 5px;">

            <a id="uploadFile" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="importF()"

                href="javascript:void(0)">上传</a> <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'"

                    href="javascript:void(0)" onclick="closeF()">关闭</a>

        </div>

        </form>

    </div>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# MVC EasyUI