1.接口(ashx)
注意需要引用:Newtonsoft.Json using System.IO;using System.Web; context.Server.UrlDecode() 用来解码ajax 传过来的值
//创建文件夹
public void CreateFolder(HttpContext context,string folderName) // folderName 文件夹名
{
string filePath = context.Server.MapPath("PaperFile/") + folderName + "/"; // PaperFile 文件夹名,为固定文件夹名
if (!Directory.Exists(filePath))
{
try
{
Directory.CreateDirectory(filePath);
HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new {Result = 200, Msg = ""}));
}
catch {HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new {Result = 400, Msg = ""}));}
}
else
{
string newfilePath = context.Server.MapPath("PaperFile/") + folderName + "(副本)/";
try
{
Directory.CreateDirectory(newfilePath);
HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { Result = 200, Msg = ""}));
}
catch {HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { Result = 400, Msg = ""}));}
}
}
//保存文件
public void SaveFile(HttpContext context, string fileName) // fileName 文件名带路径 Img/img1.jpg
{
string srcFilePath = context.Server.MapPath("PaperFile/") + fileName; //PaperFile 文件夹名,为固定文件夹名
try
{
HttpPostedFile hpfile = context.Request.Files[0];
hpfile.SaveAs(srcFilePath);
HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { Result = 200, Msg = ""}));
}
catch (Exception ex){HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { Result = 400, Msg =ex.ToString() }));}
}
//修改文件夹名称
public void UpdateFolder(HttpContext context, string oldfolderName,string newfolderName) //oldfolderName 修改前文件夹名 newfolderName 修改后文件夹名
{
string srcFolderPath= context.Server.MapPath("PaperFile/") + oldfolderName + "/";
string destFolderPath = context.Server.MapPath("PaperFile/") + newfolderName + "/";
try
{
if (Directory.Exists(srcFolderPath))
{
Directory.Move(srcFolderPath, destFolderPath);
}
HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { Result = 200, Msg = ""}));
}
catch{HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { Result = 400, Msg = ""}));}
}
//修改文件名称
public void UpdateFile(HttpContext context, string oldfileName, string newfileName) //oldfileName 前文件名 带路径 newfileName 后文件名 带路径
{
string srcFilePath = context.Server.MapPath("PaperFile/") + oldfileName;
string destFilePath = context.Server.MapPath("PaperFile/") + newfileName;
try
{
if (System.IO.File.Exists(srcFilePath))
{
System.IO.File.Move(srcFilePath,destFilePath);
}
HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { Result = 200, Msg = ""}));
}
catch(Exception ex) {HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { Result = 400, Msg = ""}));}
}
//删除文件夹
public void DeleFolder(HttpContext context, string folderName) // 需要删除的文件夹名
{
try
{
string srcFolderPath = context.Server.MapPath("PaperFile/") + folderName + "/";
if (Directory.Exists(srcFolderPath))
{
FileAttributes fileAttributes = System.IO.File.GetAttributes(srcFolderPath);
if (fileAttributes==FileAttributes.Directory)
{
Directory.Delete(srcFolderPath,true);
}
}
HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { Result = 200, Msg = ""}));
}
catch{HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { Result = 400, Msg = ""}));}
}
//删除文件
public void DeleFile(HttpContext context, string fileName) //需要删除的文件名 带路径
{
try
{
string srcFolderPath = context.Server.MapPath("PaperFile/") + fileName;
var @ref = !Directory.Exists(srcFolderPath);
if (@ref)
{
FileAttributes fileAttributes = System.IO.File.GetAttributes(srcFolderPath);
if (fileAttributes != FileAttributes.Directory)
{
System.IO.File.Delete(srcFolderPath);
}
}
HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { Result = 200, Msg = ""}));
}
catch {HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { Result = 400, Msg = ""}));}
}
//获取文件夹下面所有的文件 这里需要用到下面的文件类
public void GetDicAndFile(HttpContext context)
{
try
{
List<File> listFile = new List<File>();
string srcFolderPath = context.Server.MapPath("PaperFile/");
DirectoryInfo theFolder = new DirectoryInfo(srcFolderPath);
DirectoryInfo[] dirInfo = theFolder.GetDirectories();
//遍历文件夹
foreach (DirectoryInfo NextFolder in dirInfo)
{
//方法1
File file = new File();
file.FloderName = NextFolder.Name;
FileInfo[] fileInfo = NextFolder.GetFiles();
foreach (FileInfo NextFile in fileInfo) //遍历文件
{
file.FileName.Add(NextFile.Name);
}
listFile.Add(file);
//方法2 下面的文件类的 List<string> 属性就不用初始
File file = new File();
file.FloderName = NextFolder.Name;
FileInfo[] fileInfo = NextFolder.GetFiles();
List<string> listone=new List<string>();
foreach (FileInfo NextFile in fileInfo) //遍历文件
{
listone.Add(NextFile.Name);
}
file.FileName=listone;
listFile.Add(file);
}
HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { Result = 200, Msg = "", Data = listFile.ToArray() }));
}
catch(Exception ex) {HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { Result = 400, Msg = ""}));}
}
//下载
public void DownLoadeFile(HttpContext context,string filePath, string filename) //filePath 文件路径 filename 文件名
{
try
{
filePath = context.Server.MapPath("PaperFile/"+filePath);
if (System.IO.File.Exists(filePath))
{
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + "");
context.Response.BinaryWrite(bytes);
context.Response.Flush();
context.Response.End();
}
}
catch (IOException ex)
{
throw new Exception(ex.Message,ex);
}
}
//文件类
private class File
{
public string FloderName { get; set; }
public List<string> FileName { get; set; } = new List<string>();
}
2.ajax
//创建文件夹
function CreateFloder() {
$.ajax({
type: "get",
url: "FileManager.ashx?FolderName=" + escape("文件夹名") + "&Type=CreateFolder",
async: false,
processData: false, //必填 必须false 才会避开jq对formdata的默认处理 XMLHttpRequest才会对formdata进行正确处理 否则会报Illegal invocation错误
contentType: false, //必填 必须false 才会加上正确的Content-Type
success: function (data) {
console.log(data);
},
error: function (err) {
console.log(err);
}
});
}
//保存文件
//<input type="file" name="files" multiple="multiple" id="layui-upload" />
function SaveFile() {
var files = $('#layui-upload')[0].files; //获取上传的文件列表
var fileName = "文件夹/"+files[0].name;
var formData = new FormData(); //新建一个formData对象
for (var i = 0; i < files.length; i++) {
formData.append("files", files[i]); //append()方法
}
$.ajax({
type: "post",
url: "FileManager.ashx?FileName=" + escape(fileName) + "&Type=SaveFile",
async: false,
data: formData,
processData: false, //必填 必须false 才会避开jq对formdata的默认处理 XMLHttpRequest才会对formdata进行正确处理 否则会报Illegal invocation错误
contentType: false, //必填 必须false 才会加上正确的Content-Type
success: function (data) {
console.log(data);
},
error: function (err) {
console.log(err);
}
});
}
修改文件夹名称
function UpdateFolder() {
$.ajax({
type: "get",
url: "FileManager.ashx?OldFolderName=" + escape("文件夹") + "&NewFolderName=" + escape("文件夹副本") + "&Type=UpdateFolder",
async: false,
processData: false, //必填 必须false 才会避开jq对formdata的默认处理 XMLHttpRequest才会对formdata进行正确处理 否则会报Illegal invocation错误
contentType: false, //必填 必须false 才会加上正确的Content-Type
success: function (data) {
console.log(data);
},
error: function (err) {
console.log(err);
}
});
}
修改文件名称
function UpdateFile() {
$.ajax({
type: "get",
url: "FileManager.ashx?OldFileName=" + escape("文件夹副本/新建文本文档.txt") + "&NewFileName=" + escape("文件夹副本/新建文本文档副本.txt") + "&Type=UpdateFile",
async: false,
processData: false, //必填 必须false 才会避开jq对formdata的默认处理 XMLHttpRequest才会对formdata进行正确处理 否则会报Illegal invocation错误
contentType: false, //必填 必须false 才会加上正确的Content-Type
success: function (data) {
console.log(data);
},
error: function (err) {
console.log(err);
}
});
}
//删除文件夹
function DeleFolder() {
$.ajax({
type: "get",
url: "FileManager.ashx?FolderName=" + escape("文件夹副本/") + "&Type=DeleFolder",
async: false,
processData: false, //必填 必须false 才会避开jq对formdata的默认处理 XMLHttpRequest才会对formdata进行正确处理 否则会报Illegal invocation错误
contentType: false, //必填 必须false 才会加上正确的Content-Type
success: function (data) {
console.log(data);
},
error: function (err) {
console.log(err);
}
});
}
//删除文件
function DeleFile() {
$.ajax({
type: "get",
url: "FileManager.ashx?FileName=" + escape("文件夹副本/新建文本文档副本.txt") + "&Type=DeleFile",
async: false,
processData: false, //必填 必须false 才会避开jq对formdata的默认处理 XMLHttpRequest才会对formdata进行正确处理 否则会报Illegal invocation错误
contentType: false, //必填 必须false 才会加上正确的Content-Type
success: function (data) {
console.log(data);
},
error: function (err) {
console.log(err);
}
});
}
//获取文件夹
function GetDicAndFile() {
$.ajax({
type: "get",
url: "FileManager.ashx?Type=GetDicAndFile",
async: false,
processData: false, //必填 必须false 才会避开jq对formdata的默认处理 XMLHttpRequest才会对formdata进行正确处理 否则会报Illegal invocation错误
contentType: false, //必填 必须false 才会加上正确的Content-Type
success: function (data) {
console.log(data);
},
error: function (err) {
console.log(err);
}
});
}
//下载文件
function DownLoadeFile() {
var xhr = new XMLHttpRequest();
xhr.open("POST", "FileManager.ashx?Type=DownLoadeFile&FileName=" + escape("文件夹/新建文本文档.txt") + "&Name=" + escape("新建文本文档.txt"), true);
xhr.responseType = "blob";
xhr.onload = function () {
if (this.status == 200) {
var blob = this.response;
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onload = function (e) {
var a = document.createElement('a');
a.download = "新建文本文档.txt"; //下载时显示的文件名
a.href = e.target.result;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
}
};
xhr.send();
}