//页面JS
layui.use('upload', function () {
var $ = layui.jquery,upload = layui.upload;
// 多文件列表示例
var demoListView = $('#demoList')
, uploadListIns = upload.render({
elem: '#testList'
, url: '/api/UploadMullFiles/getTest2'
, method: 'post'
, accept: 'file'
, multiple: true
, auto: false
, bindAction: '#testListAction'
, choose: function (obj) {
var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
//读取本地文件
obj.preview(function (index, file, result) {
var tr = $(['<tr id="upload-' + index + '">'
, '<td>' + file.name + '</td>'
, '<td>' + '张三' + '</td>'
, '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
, '<td>等待上传</td>'
, '<td>'
, '<button class="layui-btn layui-btn-mini demo-reload layui-hide">重传</button>'
, '<button class="layui-btn layui-btn-mini layui-btn-danger demo-delete">删除</button>'
, '</td>'
, '</tr>'].join(''));
//单个重传
tr.find('.demo-reload').on('click', function () {
obj.upload(index, file);
});
//删除
tr.find('.demo-delete').on('click', function () {
delete files[index]; //删除对应的文件
tr.remove();
uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选
});
demoListView.append(tr);
});
}
, done: function (res, index, upload) {
debugger;
if (res[0].JOB_NAME == "上传成功!") { //上传成功
var tr = demoListView.find('tr#upload-' + index)
, tds = tr.children();
upname = upname + "$" + res[0].ORG_NAME;
uptitle = uptitle + "$" + res[0].B_NAME
tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
tds.eq(3).html(''); //清空操作
return delete this.files[index]; //删除文件队列已经上传成功的文件
}
this.error(index, upload);
}
, error: function (index, upload) {
var tr = demoListView.find('tr#upload-' + index)
, tds = tr.children();
tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
}
});
});
因为是在H5中使用所以他的跳转接口必须为控制器
/// <summary>
/// 通用上传附件信息
/// </summary>
/// <returns></returns>
[HttpPost]
public string getTest2()
{
//string id = HttpContext.Current.Request["id"];
//string name = HttpContext.Current.Request["name"];
HttpFileCollection files = HttpContext.Current.Request.Files;
Random ran = new Random();
string jsonSSS = "";
string uploadPath = "C:\\ftp\\UploadFiles\\";
foreach (string key in files.AllKeys)
{
HttpPostedFile file = files[key];//file.ContentLength文件长度
//if (string.IsNullOrEmpty(file.FileName) == false)
// file.SaveAs(HttpContext.Current.Server.MapPath(uploadPath) + file.FileName);
//return Ok("success2");
var filecombin = file.FileName.Split('.');
var tmp = file.FileName;
var tmpIndex = 0;
string FILEXX = "";
//判断是否存在相同文件名的文件 相同累加1继续判断
while (System.IO.File.Exists(uploadPath + tmp))
{
tmp = filecombin[0] + "_" + tmpIndex + "." + filecombin[1];
FILEXX = filecombin[1];
}
//不带路径的最终文件名
// FILEXX = tmp;
string RandKey = GETRandKey();
System.Threading.Thread.Sleep(100); //当前线程休息0.1秒
string name = "";
string msg = "";
string URL = "";
if (file != null)
{
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
System.Threading.Thread.Sleep(100); //当前线程休息0.1秒
string newFiles = DateTime.Now.ToString("yyMMddHHmmss") + ran.Next(1000, 9999).ToString() + "." + filecombin[1];
file.SaveAs(uploadPath + newFiles);
List<model.HHData.USERmESSAGE> lis = new List<model.HHData.USERmESSAGE>();
model.HHData.USERmESSAGE us = new model.HHData.USERmESSAGE();
us.B_NAME = file.FileName;
us.JOB_NAME = "上传成功!";
us.ORG_NAME = "ftp:\\\\888.88.88.888:8888\\UploadFiles\\" + newFiles;
//FTP 后面为IP及端口 这个根据需要更改
lis.Add(us);
jsonSSS = JsonConvert.SerializeObject(lis);
// context.Response.Write(jsonSSS);
//HttpContext.Current.Response.ContentType = "application/json";
//HttpContext.Current.Response.Charset = "utf-8";
//HttpContext.Current.Response.Write(jsonSSS);
//HttpContext context = HttpContext.Current;
//context.Response.Write(jsonSSS);
return jsonSSS;
}
}
return jsonSSS;
//return Ok(jsonSSS);
}
/// <summary>
/// 获取四位随机数
/// </summary>
/// <returns></returns>
public string GETRandKey()
{
Random ran = new Random();
int RandKey = ran.Next(1000, 9999);
return RandKey.ToString();
}
这个方法中
HttpFileCollection files = HttpContext.Current.Request.Files; 这句最重要,因为这个是获取页面的上传控件信息。。。