zoukankan      html  css  js  c++  java
  • MVC ajaxfileupload 实现无刷新导入或上传功能

    直接上代码吧

    前台

    先引用 ajaxfileupload.js

    <script src="~/Scripts/ajaxfileupload.js"></script>
    1  <input type="file" id="test_file" name="test_file" style="display:none" />
    2 
    3 <a href="#" id="test_ImportData">导入数据</a>

    这么做是为了不让那个丑的要死的file 控件出来 直接用一个按钮触发 隐藏掉原来的file 控件

     1 <script type="text/javascript">
     2     (function () {
     3         var changefile = function () {
     4             var ajaxDialog = ShowWaitMessageDialog("");//遮罩层
     5             $.ajaxFileUpload({
     6                 url: '/Home/Import',//需要链接到服务器地址
     7                 secureuri: false,
     8                 fileElementId: 'test_file',//文件选择框的id属性
     9                 dataType: 'text', //服务器返回的格式,可以是json 但是不兼容IE 故改为Text
    10                 success: function (data, status) {
    11                     //返回之后的操作
    12                     //...
    13                     HideWaitMessageDialog(ajaxDialog);//关闭提交时的弹层提示
    14                 },
    15                 complete: function (xmlHttpRequest) {
    16                     //解决点击一次file失效
    17                     $("#test_file").replaceWith('<input type="file" id="test_file" name="test_file" style="display:none"/>');
    18                     $("#test_file").change(function () {
    19                         changefile();
    20                     });
    21                 },
    22                 error: function (data, status, e) {
    23                     HideWaitMessageDialog(ajaxDialog);//关闭提交时的弹层提示
    24                     alert(e);
    25                 }
    26             });
    27         };
    28         $("#test_ImportData").click(function () {
    29             $("#test_file").click();
    30         });
    31         $("#test_file").change(function () {
    32             changefile();
    33         });
    34     }());
    35 </script>

    然后是后台接收,接收完后我是先把他存到本地,然后取出来

     1 public string Import()
     2         {
     3             try
     4             {
     5                 HttpFileCollection httpFileCollection = System.Web.HttpContext.Current.Request.Files;
     6                 string path = "";
     7                 DataTable dt = null;
     8                 if (httpFileCollection.Count > 0)
     9                 {
    10                     string fileName = httpFileCollection[0].FileName;
    11                     if (fileName.IndexOf("\") > -1)
    12                     {
    13                         //IE的情况
    14                         fileName = fileName.Substring(fileName.LastIndexOf("\") + 1);
    15                     }
    16                     path = "/UploadFiles/" + fileName;
    17                     string PhysicalPath = Server.MapPath(path);
    18                     httpFileCollection[0].SaveAs(PhysicalPath);//存到项目文件中,
    19                     dt = ExcelUtil.ReadFromCSV(PhysicalPath, true);// 通过路径读取文件内容存到datatable中
    20 
    21                 }
    22                 return JsonUtil.SerializeObject(dt);
    23             }
    24             catch (Exception ex)
    25             {
    26                 throw ex;
    27             }
    28         }
  • 相关阅读:
    重启停止的作业 bg和fg
    shell nohup 让脚本一直以后台模式运行到结束
    shell jobs查看作业
    shell 移除信号捕获
    shell 多进程运行程序
    shell 脚本后台运行
    python3 生产者消费者
    python3 生产者消费者(守护线程)
    python3 进程线程协程 并发查找列表
    python3 线程间通信
  • 原文地址:https://www.cnblogs.com/guzhanyu/p/7495576.html
Copyright © 2011-2022 走看看