1. //前台方法,包含弹出框确认以及文件选择
    <input type="button" id="importxlsx" name="importxlsx" class="k-button" style="line-height: 20px" value="@T("Admin.Common.ImportFromExcel")" />
  2.  
  3. <div id="importexcel-window" style="display:none;">
  4. @using (Html.BeginForm("ImportExcel", "ProductCarrefour", FormMethod.Post, new { name = "ImportExcelForm", enctype = "multipart/form-data" }))
  5. {
  6. <table style="text-align:left;">
  7. @*<tr>
  8. <td colspan="">
  9. <em>@T("Admin.Catalog.Products.List.ImportFromExcelTip")</em>
  10. </td>
  11. </tr>*@
  12. <tr>
  13. <td>
  14. @T("Admin.Common.ZIPFile"):
  15. </td>
  16. <td>
  17. <input type="file" id="importexcelfile" name="importexcelfile" />
  18. </td>
  19. </tr>
  20. <tr>
  21. <td colspan="">
  22. <strong>@T("Admin.Configuration.File.MaxLengthFor200")</strong>
  23. <br />
  24. <strong>@T("Admin.Configuration.Languages.XmlFile.Note1")</strong>
  25. <br />
  26. <strong>@T("Admin.Configuration.Languages.XmlFile.Note2")</strong>
  27. </td>
  28. </tr>
  29. <tr>
  30. <td colspan="">
  31. <input type="button" class="k-button" onclick="checkIsExcel(this)" value="@T("Carrefour.Admin.Controllers.GoodsController.ImportZIP")" />
  32. @*<input type="submit" id="ExcelInEnsure" style="display: none" />*@
  33. @*<input type="submit" class="k-button" value="@T("Admin.Common.ImportFromExcel")" />*@
  34. </td>
  35. </tr>
  36. </table>
  37. }
  38. </div>
  39. <script type="text/javascript">
  40. function checkIsExcel(v)
  41. {
  42. var ImportExcelForm = $("#ImportExcelForm");
  43.  
  44. var file = $("#importexcelfile").val();
  45.  
  46. if(file=="")
  47. {
  48. alert("請選擇文件");
  49. return;
  50. }
  51. var strTemp = file.split(".");
  52. var strCheck = strTemp[strTemp.length-];
  53.  
  54. if(strCheck.toUpperCase()=='ZIP'||strCheck.toUpperCase()=='zip')
  55. {
  56. //$("#ExcelInEnsure").trigger("click");
  57. submitImportProduct();
  58. }else
  59. {
  60. alert('上傳文件類型不對!');
  61. return;
  62. }
  63. }
  64. function submitImportProduct() {
  65. debugger;
  66. var formData = new FormData();
  67. formData.append("importexcelfile", $("#importexcelfile")[].files[]);
  68. $.ajax({
  69. type: 'POST',
  70. url: '@Url.Action("ImportExcel", "Product")',
  71. data: formData,
  72. cache: false,
  73. contentType: false,
  74. processData: false,
  75. success: function (data) {
  76. return false;
  77. }
  78. });
  79. $("#importexcel-window").data("kendoWindow").close();
  80. alert("文件正在異步上傳,請在上傳記錄中查看上傳狀態。")
  81. window.open("@storeLocation" + "admin/common/UploadLogList");
  82. return false;
  83. }
  84. </script>
  85. <script type="text/javascript">
  86. $(document).ready(function () {
  87. $("#importexcel").click(function (e) {
  88. e.preventDefault();
  89. var window = $("#importexcel-window");
  90. if (!window.data("kendoWindow")) {
  91. window.kendoWindow({
  92. modal: true,
  93. width: "400px",
  94. title: "@T("Admin.Common.ImportFromZIP")",
  95. actions: ["Close"]
  96. });
  97. }
  98. window.data('kendoWindow').center().open();
  99. });
  100. });
  101. </script>
  1. //后台方法
  2. [HttpPost]
  3. public ActionResult ImportExcel()
  4. {
  5. if (!_permissionService.Authorize(StandardPermissionProvider.ManageProducts) && !_permissionService.Authorize(StandardPermissionProvider.ManageVendorProducts))
  6. return AccessDeniedView();
  7.  
  8. var deleteZipFile = _settingService.GetSettingByKey("productimport.deleteproductzipfile", false);
  9. string tempFileName = CommonHelper.GetDateTimeNow().ToString("yyyyMMddHHmmssfff");
  10. var file = Request.Files["importexcelfile"];
  11. var flagFile = Server.MapPath("~/Administration//Template//" + Path.GetFileNameWithoutExtension(file.FileName));
  12.  
  13. if (System.IO.File.Exists(flagFile))
  14. {
  15. ErrorNotification(string.Format(_localizationService.GetResource("Admin.Catalog.Products.FileOnUploading"), file.FileName));
  16. return RedirectToAction("List");
  17. }
  18. FileStream myFs = new FileStream(flagFile, FileMode.Create);
  19. myFs.Close();
  20.  
  21. string filePath = Server.MapPath("~/Administration//Template//" + tempFileName + ".zip");
  22. string backupPath = Server.MapPath("~/Administration//BackupFiles//ProductImportZipFiles//");
  23. string dir = Server.MapPath("~/Administration//Template//" + tempFileName + "");
  24. try
  25. {
  26. file.SaveAs(filePath);
  27. if (!Directory.Exists(Server.MapPath("~/Administration//Template//" + tempFileName + "")))
  28. {
  29. // Create the directory it does not exist.
  30. Directory.CreateDirectory(Server.MapPath("~/Administration//Template//" + tempFileName + ""));
  31. }
  32.  
  33. //如果解压缩成功
  34. if (CopyToAndUnzipFile(filePath, tempFileName))
  35. {
  36. DirectoryInfo dirinfo = new DirectoryInfo(Server.MapPath("~/Administration//Template//" + tempFileName + ""));
  37. var afileinfo = dirinfo.GetFiles();
  38. List<string> excelCount = (from fi in afileinfo where Path.GetExtension(fi.Name).ToUpper() == ".XLS" || Path.GetExtension(fi.Name).ToUpper() == ".XLSX" select fi.FullName).ToList();
  39. if (excelCount.Count == ) //如果只存在一个excel文件
  40. {
  41.  
  42. using (FileStream fileStream = new FileStream(excelCount[], FileMode.Open, FileAccess.Read))
  43. {
  44.  
  45. _importManager.ImportProductsFromXlsxAsync(fileStream, Server.MapPath("~/Administration//Template//" + tempFileName + ""), file.FileName, filePath, deleteZipFile, flagFile, backupPath, dir);
  46.  
  47. }
  48.  
  49. return RedirectToAction("List");
  50. //}
  51. }
  52. if (excelCount.Count > )
  53. {
  54. ErrorNotification(_localizationService.GetResource("Admin.Catalog.Products.ImportProductAndGoodsExistOneMoreExcel"));
  55. }
  56. else if (excelCount.Count == )
  57. {
  58. ErrorNotification(_localizationService.GetResource("Admin.Catalog.Products.ImportProductAndGoodsExistZeroExcel"));
  59. }
  60. }
  61.  
  62. return RedirectToAction("List");
  63. }
  64. catch (Exception exc)
  65. {
  66.  
  67. TempData["ErrorMsg"] = exc.Message;
  68.  
  69. if (exc.InnerException != null)
  70. {
  71. string logError = string.Format("商品導入出錯,error:{0}", exc.InnerException.InnerException);
  72. _logger.InsertLog(LogLevel.Error, logError, logError);
  73. }
  74.  
  75. return RedirectToAction("List");
  76. }
  77. }

C#实现文件异步上传的更多相关文章

  1. HTML5实现图片文件异步上传

    原文:HTML5实现图片文件异步上传 利用HTML5的新特点做文件异步上传非常简单方便,本文主要展示JS部分,html结构.下面的代码并未使用第三发库,如果有参照,请注意一些未展现出来的代码片段.我这 ...

  2. 文件的上传(表单上传和ajax文件异步上传)

    项目中用户上传总是少不了的,下面就主要的列举一下表单上传和ajax上传!注意: context.Request.Files不适合对大文件进行操作,下面列举的主要对于小文件上传的处理! 资源下载: 一. ...

  3. js 文件异步上传 显示进度条 显示上传速度 预览文件

    通常文件异步提交有几个关键 1.支持拖拽放入文件.2.限制文件格式.3.预览图片文件.4.上传进度,速度等,上传途中取消上传.5.数据与文件同时上传 现在开始笔记: 需要一个最基础的元素<inp ...

  4. 普通文件的上传(表单上传和ajax文件异步上传)

    一.表单上传: html客户端部分: <form action="upload.ashx" method="post" enctype="mul ...

  5. MVC文件上传04-使用客户端jQuery-File-Upload插件和服务端Backload组件实现多文件异步上传

    本篇使用客户端jQuery-File-Upload插件和服务端Badkload组件实现多文件异步上传.MVC文件上传相关兄弟篇: MVC文件上传01-使用jquery异步上传并客户端验证类型和大小  ...

  6. SpringMVC + AJAX 实现多文件异步上传

    转自:https://www.jianshu.com/p/f3987f0f471f 今天,我就这个问题来写一篇如何用 SpringMVC + AJAX 实现的多文件异步上传功能.基本的代码还是沿用上篇 ...

  7. 文件的上传(1)(表单上传和ajax文件异步上传)

    文件的上传(表单上传和ajax文件异步上传) 项目中用户上传总是少不了的,下面就主要的列举一下表单上传和ajax上传!注意: context.Request.Files不适合对大文件进行操作,下面列举 ...

  8. 小程序使用 Promise.all 完成文件异步上传

    小程序使用 Promise.all 完成文件异步上传 extends [微信小程序开发技巧总结(二) -- 文件的选取.移动.上传和下载 - Kindear - 博客园 (cnblogs.com)] ...

  9. 文件上传之——用SWF插件实现文件异步上传和头像截取

    之前写过几篇文件上传,那些都不错.今天小编带领大家体会一种新的上传方法,及使用Flash插件实现文件上传. 使用Flash的好处就是可以解决浏览器兼容性问题.之前我写的一个快捷复制功能也是利用的Fla ...

  10. HTML5 文件异步上传 — h5uploader.js

    原文地址:http://imziv.com/blog/article/read.htm?id=62 之前写过一篇H5异步文件上传的文章, 但是很多朋友看着我的这个教程还是出现很多问题,文章写的不是很好 ...

随机推荐

  1. O(1)乘法与快速乘O(log)

    //O(1)快速乘 inline LL quick_mul(LL x,LL y,LL MOD){ x=x%MOD,y=y%MOD; return ((x*y-(LL)(((long double)x* ...

  2. 螺旋队列(p98)

    先判断这个坐标代表的数位于哪一层,然后依据该层最大的数去计算这个坐标所代表的数. #include"iostream" #include"stdio.h" #i ...

  3. PIE SDK云图动画导出

    云图动画,就是将一组序列图以动画的形式进行轮播,PIE SDK可以将云图动画以gif或avi格式进行导出,本文示例以云图动画导出gif为例,这样只需要点开gif文件就可以浏览云图动画. 下面来介绍下实 ...

  4. Git merge rebase cherry-pick 以及 游离commit 的测试

    Microsoft Windows [版本 10.0.17134.345] (c) Microsoft Corporation.保留所有权利. C:\Users\zhangyang\Desktop\b ...

  5. oracle常用DDL语句

    1.添加表字段--咨询表添加内容简介字段 ALTER TABLE s_table ADD intro VARCHAR2(1024); COMMENT ON COLUMN s_table.remarks ...

  6. Python 字典的取值

    不能用.取值 .是获取属性或方法 只能用中括号或者get方法 中括号和get中可以放字符串或者变量 get与[]的区别在于当key不存在,get不会报错,而且get可以设置取不到值时返回的默认值.

  7. mysql 查询及 删除表中重复数据

    CREATE TABLE `test` ( `id` INT(20) NOT NULL AUTO_INCREMENT, `name` VARCHAR(20) NULL DEFAULT NULL, `a ...

  8. 聚焦游戏安全,腾讯云GAME-TECH“空降”上海

    游戏行业是DDoS攻击高发行业,占DDoS攻击的六成以上,特别是近年来游戏行业的爆发式增长,游戏行业更成为了黑产.外挂.非法信息的聚集地.安全,已然成为游戏行业当前最大的敌人. 6月29日,腾讯云GA ...

  9. HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】

    Bipartite Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  10. 很有用的PHP笔试题系列二

    1.如何用php的环境变量得到一个网页地址的内容?ip地址又要怎样得到? Gethostbyname() echo $_SERVER ["PHP_SELF"];echo $_SER ...