文件文件夹拖拽上传
请把文件拖动到这里来
2.引入layer.js
上传事件
//上传文件
function _upload() {
var loadid = layer.load();
var itemfunc = function (item) {
if (item) {
var fdata = new FormData();
fdata.append("fullPath", item.fullPath);
fdata.append("time", (new Date()).getTime());
fdata.append("name", item.name);
fdata.append(item.name, item);
fdata.append("lastModified", item.lastModified);
$.ajax({
url: "/api/file/dropupload",
type: 'post',
contentType: false,
processData: false,
async: true,
data: fdata,
headers: { "token": '112233' },
success: function (obj) {
console.log(obj);
},
error: function (res) {
console.error(es);
}, complete: function () {
if (_selectfilelist.length > 0) {
itemfunc(_selectfilelist.shift());
} else {
layer.close(loadid);
console.log("上载完成");
}
}
});
}
};
if (_selectfilelist.length > 0) {
itemfunc(_selectfilelist.shift());
} else {
layer.close(loadid);
console.log("上载完成");
}
}
服务端接收文件
///
///
///
///
[HttpPost]
[Route("/api/file/dropupload")]
public dynamic drop()
{
var result = false;
var msg = String.Empty;
if (base.Request.Form != null)
{
var lastMillSecond = DateTime.Now.ToUnixTimeMilliseconds();
var fullPath = String.Empty;
//这里还需要换算时差
if (base.Request.Form.ContainsKey("lastModified"))
{
long.TryParse(base.Request.Form["lastModified"], out lastMillSecond);
}
if (base.Request.Form.ContainsKey("fullPath"))
{
fullPath = base.Request.Form["fullPath"].ToString();
}
if (lastMillSecond == 0) { lastMillSecond = DateTime.Now.ToUnixTimeMilliseconds(); }
if (base.Request.Form.Files != null)
{
if (base.Request.Form.Files.Count > 0)
{
for (var k = 0; k < base.Request.Form.Files.Count; k++)
{
var finput = base.Request.Form.Files[k];
var save = $"/app/wwwroot/temp{fullPath}";
var path = System.IO.Path.GetDirectoryName(save);
if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); }
System.IO.File.Delete(save);
using (System.IO.FileStream fs = new System.IO.FileStream(save, System.IO.FileMode.Create))
{
finput.CopyTo(fs);
fs.Flush();
fs.Dispose();
}
var finfo = new System.IO.FileInfo(save);
finfo.LastWriteTime = lastMillSecond.ToDateTimeFromMilliseconds();
result = true;
msg = save;
}
}
}
}
return new
{
result = result,
msg = msg
};
}
读取现有的文件递归读取下级
///
/// read linux path files!
///
///
///
[HttpGet]
[Route("/api/file/dropread")]
public dynamic dropread(string path="/app/wwwroot/temp")
{
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
var list = new List();
ReadFile(path, list,path);
return list;
}
///
/// 从文件夹中读取文件 递归
///
///
///
///
private void ReadFile(string path,List list,string root)
{
if (path.Contains(@"\")) { path = path.Replace(@"\", "/"); }
if (System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
else
{
Console.WriteLine($"path:{path} not exists!");
}
Console.WriteLine($"begin read path:{path}");
var dir = new System.IO.DirectoryInfo(path);
var dirs = dir.GetDirectories();
foreach (var item in dirs)
{
ReadFile(CombinePath(path,item.Name),list,root);
}
var files = dir.GetFiles();
foreach (var item in files)
{
var one = new DropFileItem()
{
fullPath = ReadRelativePath(root, item.DirectoryName) + item.Name,
lastModified = item.LastWriteTime.ToLocalMilliseconds(),
name = item.Name
};
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(one));
list.Add(one);
}
}
///
/// 合并路径
///
///
///
///
private string CombinePath(string headpath, string footpath)
{
if (!headpath.StartsWith("/") && !headpath.StartsWith(@"\"))
{
headpath = "/" + headpath;
}
if (headpath.Contains(@"\"))
{
headpath = headpath.Replace(@"\", "/");
}
if (headpath.Contains(@"\\"))
{
headpath = headpath.Replace(@"\\", "/");
}
if (headpath.Contains("//"))
{
headpath = headpath.Replace("//", "/");
}
if (!footpath.StartsWith("/") && !footpath.StartsWith(@"\"))
{
footpath = "/" + footpath;
}
if (footpath.Contains(@"\"))
{
footpath = footpath.Replace(@"\", "/");
}
if (footpath.Contains(@"\\"))
{
footpath = footpath.Replace(@"\\", "/");
}
if (footpath.Contains("//"))
{
footpath = footpath.Replace("//", "/");
}
if (!headpath.EndsWith("/")) { headpath = headpath + "/"; }
return $"{headpath}{footpath}";
}
///
/// 获取相对路径
///
///
///
///
private string ReadRelativePath(string rootpath,string fullpath)
{
//Console.WriteLine($"{fullpath} ---> {rootpath}");
fullpath = fullpath.Replace(@"D:\","/");
//Console.WriteLine($"{fullpath} ---> {rootpath}");
if (!fullpath.StartsWith("/") && !fullpath.StartsWith(@"\"))
{
fullpath = "/"+fullpath;
}
if (fullpath.Contains(@"\"))
{
fullpath = fullpath.Replace(@"\","/");
}
if (!rootpath.StartsWith("/") && !rootpath.StartsWith(@"\"))
{
rootpath = "/" + rootpath;
}
if (rootpath.Contains(@"\"))
{
rootpath = rootpath.Replace(@"\", "/");
}
if (rootpath.Contains(@"\\"))
{
rootpath = rootpath.Replace(@"\\", "/");
}
if (rootpath.Contains("//"))
{
rootpath = rootpath.Replace("//", "/");
}
if (fullpath.Contains(@"\\"))
{
fullpath = fullpath.Replace(@"\\", "/");
}
if (fullpath.Contains("//"))
{
fullpath = fullpath.Replace("//", "/");
}
// //->/ \\->/
//以下判定还是 有BUG的
if (fullpath.Contains(rootpath))
{
var relapath = fullpath.Substring(rootpath.Length);
if(!relapath.StartsWith("/"))
{
relapath = "/" + relapath;
}
if (!relapath.EndsWith("/"))
{
relapath = relapath + "/";
}
//Console.WriteLine($"=== {fullpath} --- {rootpath} ===> {relapath}");
return relapath;
}
return fullpath;
}
注意!!! 以上路径都是在linux上的,如果用window需要改改,问题不大。