开发环境:WIN10+IE11,浏览器请使用IE10或以上版本

开发技术框架MVC4+JQuery Easyui+knockoutjs

效果为弹出小窗体,如下图

1.前端cshtml文件代码(只包含文件上传窗体)。注意form设置,必须使用form-data传递文件。注意按钮事件我这里是封装的data-bind="click:closeImportClick",不要照抄

html5可直接在input标签file控件中设置accept属性限制上传文件类型,设置multiple属性可同时上传多个文件

<div class="easyui-window" id="import-excel-template" title="文件上传" style="width:400px;height:160px;padding:2px;" closed="true">
<form id="importFileForm" method="post" enctype="multipart/form-data" style="display:none">
<table style="margin:5px;height:70px;">
<tr>
<td>请选择文件</td>
<td width="5px;"></td>
<td><input type="file" class="easyui-filebox" id="fileImport" name="fileImport" 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'" data-bind="click:importFileClick" href="javascript:void(0)">上传</a>
<a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" data-bind="click:closeImportClick" href="javascript:void(0)">关闭</a>
</div>
</form>
</div>

2.ViewModel中js代码:定义上传事件。注意使用ajax请求时,需要设置contentType: false,否则chrome和firefox不兼容

    //导入事件,显示导入弹出窗口
this.importClick = function ()
{
        $('#import-excel-template').window('open')
     document.getElementById("importFileForm").style.display = "block";
}
//关闭导入弹出窗口
this.closeImportClick = function () {
document.getElementById('fileImport').value = null;
document.getElementById('fileName').innerHTML = "";
document.getElementById('uploadInfo').innerHTML = "";
$('#import-excel-template').window('close')
} //导入文件
this.importFileClick = function ()
{
//获取上传文件控件内容
var file = document.getElementById('fileImport').files[0];
//判断控件中是否存在文件内容,如果不存在,弹出提示信息,阻止进一步操作
if (file == null) { alert('错误,请选择文件'); return; }
//获取文件名称
var fileName = file.name;
//获取文件类型名称
var file_typename = fileName.substring(fileName.lastIndexOf('.'), fileName.length);
//这里限定上传文件文件类型必须为.xlsx,如果文件类型不符,提示错误信息
if (file_typename == '.xlsx')
{
//计算文件大小
var fileSize = 0;
//如果文件大小大于1024字节X1024字节,则显示文件大小单位为MB,否则为KB
if (file.size > 1024 * 1024) {

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

         if (fileSize > 10) {
alert('错误,文件超过10MB,禁止上传!'); return;
}
         fileSize = fileSize.toString() + 'MB';
}
else {
fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';
}
//将文件名和文件大小显示在前端label文本中
document.getElementById('fileName').innerHTML = "<span style='color:Blue'>文件名: " + file.name + ',大小: ' + fileSize + "</span>";
//获取form数据
var formData = new FormData($("#importFileForm")[0]);
//调用apicontroller后台action方法,将form数据传递给后台处理。contentType必须设置为false,否则chrome和firefox不兼容
$.ajax({
url: "/api/Product/NewMaterialImport/PostExcelData",
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (returnInfo) {
//上传成功后将控件内容清空,并显示上传成功信息
document.getElementById('fileImport').value = null;
document.getElementById('uploadInfo').innerHTML = "<span style='color:Red'>" + returnInfo + "</span>";
},
error: function (returnInfo) {
//上传失败时显示上传失败信息
document.getElementById('uploadInfo').innerHTML = "<span style='color:Red'>" + returnInfo + "</span>";
}
});
}
else {
alert("文件类型错误");
//将错误信息显示在前端label文本中
document.getElementById('fileName').innerHTML = "<span style='color:Red'>错误提示:上传文件应该是.xlsx后缀而不应该是" + file_typename + ",请重新选择文件</span>"
}
}

3.apicontroller代码

/// <summary>
/// 将文件上传到指定路径中保存
/// </summary>
/// <returns>上传文件结果信息</returns>
[System.Web.Http.HttpPost]
[ValidateInput(false)]
public string PostExcelData()
{
string info = string.Empty;
try
{
//获取客户端上传的文件集合
HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
//判断是否存在文件
if (files.Count > )
{
//获取文件集合中的第一个文件(每次只上传一个文件)
HttpPostedFile file = files[];
//定义文件存放的目标路径
string targetDir = System.Web.HttpContext.Current.Server.MapPath("~/FileUpLoad/Product");
//创建目标路径
ZFiles.CreateDirectory(targetDir);
//组合成文件的完整路径
string path = System.IO.Path.Combine(targetDir, System.IO.Path.GetFileName(file.FileName));
//保存上传的文件到指定路径中
file.SaveAs(path);
info = "上传成功";
}
else
{
info = "上传失败";
}
}
catch
{
info= "上传失败";
}
return info;
}

MVC项目使用easyui的filebox控件上传文件的更多相关文章

  1. c#上传文件(一)使用 .net 控件上传文件

    1.html代码: <body> <form id="form1" runat="server"> <div> <as ...

  2. python3、selenium、autoit3,通过flash控件上传文件

    autoit.au3 #include <Constants.au3> WinWait(); //暂停执行脚本,直到上传对话框出现 WinActive("打开") Wi ...

  3. asp.net FileUpload 控件上传文件 以二进制的形式存入数据库并将图片显示出来

    图片上传事件代码如下所示: byte[] binary = upload.FileBytes; StringBuilder sqlStrSb = new StringBuilder(); sqlStr ...

  4. 在Update Panel 控件里面添加 File Upload 控件 上传文件

    Detail Information:http://www.codeproject.com/Articles/482800/FileplusUploadplusinplusUpdateplusPane ...

  5. FileUpload的控件上传excel

    在一个使用FileUpload的控件上传excel,读取excel的数据 因为上传的路径一直被限定在C:\Program\IIS\Express 一直限制这个文件下, 想要解决这个问题. 在谷歌浏览器 ...

  6. .net简单的fileupload控件上传

    前台代码: <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID ...

  7. JS ajaxfileUpload 一次性上传多个input控件 上传多个文件

    本方法适用于一次性上传多个input框输入的文件,如下图所示,任务是需要一次上传两个input框提供的两个文件. 具体方法: 1.修改ajax调用方法 如上图所示,只需要将ajaxFileUpload ...

  8. WebForm使用FileUpload控件上传压缩二进制图片

    fuImage 是FileUpload页面控件 ImageHelper.CompressionImage(fuImage.FileBytes, quality); /// <summary> ...

  9. 【SpringMVC】【EasyUI】关于使用EasyUIForm上传文件,返回JsonIE提示下载文件的解决办法!

    先说一下环境 EasyUI+SpringMVC+MyBatis 因为按正常手段,无法使用Ajax来提交一个包含文件的表单,故想到利用EasyUI的Form来提交,EasyUI的form封装了一套伪Aj ...

随机推荐

  1. POJ2794 Double Patience[离散概率 状压DP]

    Double Patience Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 694   Accepted: 368 Cas ...

  2. JPA中entityManager的CRUD

    private EntityManagerFactory entityManagerFactory; private EntityManager entityManager; private Enti ...

  3. 如何用Maven创建web项目

    使用eclipse插件创建一个web project 首先创建一个Maven的Project如下图 我们勾选上Create a simple project (不使用骨架) 这里的Packing 选择 ...

  4. Revolving Digits[EXKMP]

    Revolving Digits Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. 【C#】【Thread】Barrier任务并行

    Barrier  是一个对象,它可以在并行操作中的所有任务都达到相应的关卡之前,阻止各个任务继续执行. 如果并行操作是分阶段执行的,并且每一阶段要求各任务之间进行同步,则可以使用该对象. --MSDN ...

  6. 常用Git代码托管服务分享

    Git Repository代码托管服务越来越流行,目前有很多商业公司和个人团队逐渐切换项目到 Git平台进行代码托管.本文分享一些常用的Git代码托管服务,其中一些提供私有项目保护服务,特别有利于远 ...

  7. WPF Popup 控件导致被遮挡内容不刷新的原因

    WPF Popup 控件导致被遮挡内容不刷新的原因 周银辉 今天在写一个WPF控件时用到了Popup控件,很郁闷的情况是:当popup关闭时,原来被popup挡住的界面部分不刷新,非要手动刷新一下(比 ...

  8. RequireJS shim 用法说明

    RequireJS中如果使用AMD规范,在使用的过程中没有太多的问题,如果加载非AMD规范的JS文件,就需要使用Require中的shim. require.config({ paths:{ jque ...

  9. docfx组件介绍--YamlSerialization

    在docfx中把元数据以yaml的形式保存,在metadata阶段会序列化数据到yaml文件中,在build阶段又需要从yaml文件反序列化出来.在使用过程中,意外发现yamldotnet在处理大量强 ...

  10. 风口之下,猪都能飞。当今中国股市牛市,真可谓“错过等七年”。 给你一个回顾历史的机会,已知一支股票连续n天的价格走势,以长度为n的整数数组表示,

    转自:http://www.cnblogs.com/ranranblog/p/5845010.html 风口之下,猪都能飞.当今中国股市牛市,真可谓“错过等七年”. 给你一个回顾历史的机会,已知一支股 ...