概述
不同于上次文件上传,本次上传在js中提交,且上传后重命名文件并存名与数据库
HTML代码
<div>
<form id="form" name="form" method="post" enctype="multipart/form-data" >
<div class="container_16" style="width:90%;margin:5%;">
<div class="grid_3 lbl">选择员工</div>
<div>
<input id="txtFullName" name="FullName" class="easyui-combogrid" style="width: 220px" />
<input id="txtEmpId" name="EmpId" class="z-txt readonly" readonly="readonly" style="width: 200px" type="hidden" />
</div>
<div class="clear"></div>
<div class="grid_3 lbl">选择文件</div>
<div class="grid_13 val">
<input type="file" class="easyui-filebox" id="txtUpload"multiple="multiple" name="fileImport" style="width:260px;">
<a id="uploadFile" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="#">上传</a>
</div>
</div>
</form>
<div style="text-align:center;margin-top:15px">
<a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" id="txtSure" data-bind="click:confirmClick" href="javascript:void(0)">保存</a>
<a class="easyui-linkbutton" id="btnCancel" data-options="iconCls:'icon-cancel'" data-bind="click:cancelClick" href="javascript:void(0)">取消</a>
</div>
<div class="clear"></div>
<p align="center" ><font color="#B22222">注:为避免重名,系统会自动重命名文件</font></p>
</div>
js代码
//点击上传按钮
$("#uploadFile").bind('click', function (e) {
var file = parent.$("#txtUpload").prop('files');
var size = 0;
if (file.length == 0) {
com.message('warning', '请选择文件!');
return false;
} else {
for (var i = 0; i < file.length; i++) {
size += file[i].size;
}
if (size / 1024 / 1024 > 25) {
com.message('warning', '文件过大,请上传小于25M的文件!');
return false;
}
for (var i = 0; i < file.length; i++) {
var l = file[i].name.split(".").length;
var type = file[i].name.split(".")[l - 1];
if (type == "doc" || type == "docx" || type == "xls" || type == "xlsx" || type == "ppt" || type == "pptx" || type == "pdf" || type == "jpg" || type == "bmp" || type == "gif" || type == "png" || type == "txt" || type == "zip" || type == "rar") {
} else {
$.messager.alert('提示', ' 暂不支持上传' + type + '文件,请重新选择!', 'info');
return false;
}
}
//核心!!!
var formData = new FormData($("#form")[0]);
$.ajax({
url: "/api/base/ContractFile/PostData",
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (d) {
//将后台返回的数据存于缓存,通过window.localStorage.getItem("ContractFileName");获取d
window.localStorage.setItem("ContractFileName", d);
$.messager.alert('提示', '上传成功,记得点下方保存哦', 'info');
},
error: function (d) {
$.messager.alert('提示', '上传失败', 'info');
}
});
}
});
后台代码
[System.Web.Http.HttpPost]
public List<dynamic> PostData()
{
string pathForSaving = HttpContext.Current.Server.MapPath(string.Format("~/{0}", @"TempContract"));
HttpFileCollection files = HttpContext.Current.Request.Files;
string tempName = "";
string path="";
dynamic result = new ExpandoObject();
List<dynamic> dt = new List<dynamic>();
for (int i = 0; i < files.Count; i++)
{
string type = files[i].FileName.Substring(files[i].FileName.LastIndexOf(".") + 1);
if (type == "doc" || type == "docx" || type == "xls" || type == "xlsx" || type == "ppt" || type == "pptx" || type == "pdf" || type == "jpg" || type == "bmp" || type == "gif" || type == "png" || type == "txt" || type == "zip" || type == "rar")
{
tempName = Path.GetFileNameWithoutExtension(files[i].FileName) + DateTime.Now.ToString("_yyyyMMddHHmmssfff") + Path.GetExtension(files[i].FileName);
path = Path.Combine(pathForSaving, tempName);
try
{
files[i].SaveAs(path);
result.msg = "success";
dt.Add(tempName);
}
catch (Exception)
{
result.msg = "error";
}
}
}
return dt;
}
上面只贴出了上传按钮实现代码,保存按钮用于将上传后的文件名及对应员工存于数据库
最后
以上就是敏感服饰为你收集整理的asp.net MVC+easyUI 非form表单提交上传文件的全部内容,希望文章能够帮你解决asp.net MVC+easyUI 非form表单提交上传文件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复