C#实现文件异步上传
//前台方法,包含弹出框确认以及文件选择
<input type="button" id="importxlsx" name="importxlsx" class="k-button" style="line-height: 20px" value="@T("Admin.Common.ImportFromExcel")" /> <div id="importexcel-window" style="display:none;">
@using (Html.BeginForm("ImportExcel", "ProductCarrefour", FormMethod.Post, new { name = "ImportExcelForm", enctype = "multipart/form-data" }))
{
<table style="text-align:left;">
@*<tr>
<td colspan="">
<em>@T("Admin.Catalog.Products.List.ImportFromExcelTip")</em>
</td>
</tr>*@
<tr>
<td>
@T("Admin.Common.ZIPFile"):
</td>
<td>
<input type="file" id="importexcelfile" name="importexcelfile" />
</td>
</tr>
<tr>
<td colspan="">
<strong>@T("Admin.Configuration.File.MaxLengthFor200")</strong>
<br />
<strong>@T("Admin.Configuration.Languages.XmlFile.Note1")</strong>
<br />
<strong>@T("Admin.Configuration.Languages.XmlFile.Note2")</strong>
</td>
</tr>
<tr>
<td colspan="">
<input type="button" class="k-button" onclick="checkIsExcel(this)" value="@T("Carrefour.Admin.Controllers.GoodsController.ImportZIP")" />
@*<input type="submit" id="ExcelInEnsure" style="display: none" />*@
@*<input type="submit" class="k-button" value="@T("Admin.Common.ImportFromExcel")" />*@
</td>
</tr>
</table>
}
</div>
<script type="text/javascript">
function checkIsExcel(v)
{
var ImportExcelForm = $("#ImportExcelForm"); var file = $("#importexcelfile").val(); if(file=="")
{
alert("請選擇文件");
return;
}
var strTemp = file.split(".");
var strCheck = strTemp[strTemp.length-]; if(strCheck.toUpperCase()=='ZIP'||strCheck.toUpperCase()=='zip')
{
//$("#ExcelInEnsure").trigger("click");
submitImportProduct();
}else
{
alert('上傳文件類型不對!');
return;
}
}
function submitImportProduct() {
debugger;
var formData = new FormData();
formData.append("importexcelfile", $("#importexcelfile")[].files[]);
$.ajax({
type: 'POST',
url: '@Url.Action("ImportExcel", "Product")',
data: formData,
cache: false,
contentType: false,
processData: false,
success: function (data) {
return false;
}
});
$("#importexcel-window").data("kendoWindow").close();
alert("文件正在異步上傳,請在上傳記錄中查看上傳狀態。")
window.open("@storeLocation" + "admin/common/UploadLogList");
return false;
}
</script>
<script type="text/javascript">
$(document).ready(function () {
$("#importexcel").click(function (e) {
e.preventDefault();
var window = $("#importexcel-window");
if (!window.data("kendoWindow")) {
window.kendoWindow({
modal: true,
width: "400px",
title: "@T("Admin.Common.ImportFromZIP")",
actions: ["Close"]
});
}
window.data('kendoWindow').center().open();
});
});
</script>
//后台方法
[HttpPost]
public ActionResult ImportExcel()
{
if (!_permissionService.Authorize(StandardPermissionProvider.ManageProducts) && !_permissionService.Authorize(StandardPermissionProvider.ManageVendorProducts))
return AccessDeniedView(); var deleteZipFile = _settingService.GetSettingByKey("productimport.deleteproductzipfile", false);
string tempFileName = CommonHelper.GetDateTimeNow().ToString("yyyyMMddHHmmssfff");
var file = Request.Files["importexcelfile"];
var flagFile = Server.MapPath("~/Administration//Template//" + Path.GetFileNameWithoutExtension(file.FileName)); if (System.IO.File.Exists(flagFile))
{
ErrorNotification(string.Format(_localizationService.GetResource("Admin.Catalog.Products.FileOnUploading"), file.FileName));
return RedirectToAction("List");
}
FileStream myFs = new FileStream(flagFile, FileMode.Create);
myFs.Close(); string filePath = Server.MapPath("~/Administration//Template//" + tempFileName + ".zip");
string backupPath = Server.MapPath("~/Administration//BackupFiles//ProductImportZipFiles//");
string dir = Server.MapPath("~/Administration//Template//" + tempFileName + "");
try
{
file.SaveAs(filePath);
if (!Directory.Exists(Server.MapPath("~/Administration//Template//" + tempFileName + "")))
{
// Create the directory it does not exist.
Directory.CreateDirectory(Server.MapPath("~/Administration//Template//" + tempFileName + ""));
} //如果解压缩成功
if (CopyToAndUnzipFile(filePath, tempFileName))
{
DirectoryInfo dirinfo = new DirectoryInfo(Server.MapPath("~/Administration//Template//" + tempFileName + ""));
var afileinfo = dirinfo.GetFiles();
List<string> excelCount = (from fi in afileinfo where Path.GetExtension(fi.Name).ToUpper() == ".XLS" || Path.GetExtension(fi.Name).ToUpper() == ".XLSX" select fi.FullName).ToList();
if (excelCount.Count == ) //如果只存在一个excel文件
{ using (FileStream fileStream = new FileStream(excelCount[], FileMode.Open, FileAccess.Read))
{ _importManager.ImportProductsFromXlsxAsync(fileStream, Server.MapPath("~/Administration//Template//" + tempFileName + ""), file.FileName, filePath, deleteZipFile, flagFile, backupPath, dir); } return RedirectToAction("List");
//}
}
if (excelCount.Count > )
{
ErrorNotification(_localizationService.GetResource("Admin.Catalog.Products.ImportProductAndGoodsExistOneMoreExcel"));
}
else if (excelCount.Count == )
{
ErrorNotification(_localizationService.GetResource("Admin.Catalog.Products.ImportProductAndGoodsExistZeroExcel"));
}
} return RedirectToAction("List");
}
catch (Exception exc)
{ TempData["ErrorMsg"] = exc.Message; if (exc.InnerException != null)
{
string logError = string.Format("商品導入出錯,error:{0}", exc.InnerException.InnerException);
_logger.InsertLog(LogLevel.Error, logError, logError);
} return RedirectToAction("List");
}
}
C#实现文件异步上传的更多相关文章
- HTML5实现图片文件异步上传
原文:HTML5实现图片文件异步上传 利用HTML5的新特点做文件异步上传非常简单方便,本文主要展示JS部分,html结构.下面的代码并未使用第三发库,如果有参照,请注意一些未展现出来的代码片段.我这 ...
- 文件的上传(表单上传和ajax文件异步上传)
项目中用户上传总是少不了的,下面就主要的列举一下表单上传和ajax上传!注意: context.Request.Files不适合对大文件进行操作,下面列举的主要对于小文件上传的处理! 资源下载: 一. ...
- js 文件异步上传 显示进度条 显示上传速度 预览文件
通常文件异步提交有几个关键 1.支持拖拽放入文件.2.限制文件格式.3.预览图片文件.4.上传进度,速度等,上传途中取消上传.5.数据与文件同时上传 现在开始笔记: 需要一个最基础的元素<inp ...
- 普通文件的上传(表单上传和ajax文件异步上传)
一.表单上传: html客户端部分: <form action="upload.ashx" method="post" enctype="mul ...
- MVC文件上传04-使用客户端jQuery-File-Upload插件和服务端Backload组件实现多文件异步上传
本篇使用客户端jQuery-File-Upload插件和服务端Badkload组件实现多文件异步上传.MVC文件上传相关兄弟篇: MVC文件上传01-使用jquery异步上传并客户端验证类型和大小 ...
- SpringMVC + AJAX 实现多文件异步上传
转自:https://www.jianshu.com/p/f3987f0f471f 今天,我就这个问题来写一篇如何用 SpringMVC + AJAX 实现的多文件异步上传功能.基本的代码还是沿用上篇 ...
- 文件的上传(1)(表单上传和ajax文件异步上传)
文件的上传(表单上传和ajax文件异步上传) 项目中用户上传总是少不了的,下面就主要的列举一下表单上传和ajax上传!注意: context.Request.Files不适合对大文件进行操作,下面列举 ...
- 小程序使用 Promise.all 完成文件异步上传
小程序使用 Promise.all 完成文件异步上传 extends [微信小程序开发技巧总结(二) -- 文件的选取.移动.上传和下载 - Kindear - 博客园 (cnblogs.com)] ...
- 文件上传之——用SWF插件实现文件异步上传和头像截取
之前写过几篇文件上传,那些都不错.今天小编带领大家体会一种新的上传方法,及使用Flash插件实现文件上传. 使用Flash的好处就是可以解决浏览器兼容性问题.之前我写的一个快捷复制功能也是利用的Fla ...
- HTML5 文件异步上传 — h5uploader.js
原文地址:http://imziv.com/blog/article/read.htm?id=62 之前写过一篇H5异步文件上传的文章, 但是很多朋友看着我的这个教程还是出现很多问题,文章写的不是很好 ...
随机推荐
- POJ3635 Full Tank? 优先队列BFS or 分层图最短路 or DP?
然而我也不知道这是啥啊...反正差不多...哪位大佬给区分一下QWQ.. 好的,我把堆的<写反了..又调了一个小时..你能不能稳一点.... 记录状态:所在位置u,油量c,花费w 扩展状态: 1 ...
- bzoj1076 奖励关 期望dp
题目传送门 题目大意:总共有k次弹出宝物的机会,宝物共有n种,弹出不同的宝物的概率相同的,是每个宝物都有价值,和选择这个宝物的限制(必须具有特定的宝物),问最后的最优期望是多少. 思路:“正向推概率, ...
- 江西理工大学南昌校区排名赛 E: 单身狗的种树游戏
题目描述 萌樱花是一只单身狗. 萌樱花今天决定种树,于是他来到了自己家的后院. 萌樱花的后院有n个树坑,所有树坑排列在一行上,每一个树坑都可以种一棵树,相邻树坑间的距离为1,现在所有的树坑都是空着的. ...
- 移动端数据爬取(fidlde)
一.什么是Fiddler? 1 什么是Fiddler? Fiddler是位于客户端和服务器端的HTTP代理,也是目前最常用的http抓包工具之一 . 它能够记录客户端和服务器之间的所有 HTTP请求, ...
- EntityFramework 建立一对一关系
前言:本来要使用实体拆分实现一对一,但发现查询时无法单独查询,影响效率,故改用手动建立一对一关系 例: 实体类: public class TestDbContext : DbContext { pu ...
- PIE SDK矢量数据编辑事件的监听
1.功能简介 通过IEditEvents接口,开发者可以监听到Editor对象的相关的事件,并且做出反应.包括Editor中开始编辑.结束编辑等操作,下面对矢量数据的编辑事件的监听功能进行介绍. 2. ...
- [转]ClassPath是什么
from: https://my.oschina.net/GivingOnenessDestiny/blog/603505 classpath 是什么classpath实际上就是编译后的 以 clas ...
- Android中的ListView点击时的背景颜色设置
想设置listview中每行在点击.选中等不同状态下有不同的背景颜色,或者背景图片. 这可以用Android的Selector来实现.它可以定义组件在不同状态下的显示方式. 新建一个xml文件list ...
- Android的Intent和IntentFilter应用说明一例
很多人对文档中的Intent和IntentFilter不理解是什么意思,我这里举例解释下. Intent字面意思就是目标,目的.通俗一点,需要达成某些目标,则需要提供一些动作,这些目标的分类,以及达成 ...
- RabbitMQ学习整理
1.什么是消息队列? 概念: 消息队列(Message Queue,简称MQ),本质是个队列,FIFO先入先出,只不过队列中存放的内容是一些Message. 2.为什么要用消息队列,应用场景? 不同系 ...