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

 <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'" οnclick="importF()"
                href="javascript:void(0)">上传</a> <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'"
                    href="javascript:void(0)" οnclick="closeF()">关闭</a>
        </div>
        </form>
    </div>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值