用API完成实体间传输,批量挂接
我用的是vs2019,直接用的是asp.net core Web应用程序里面的API
我们先把api写好
- 首先我要考虑如果传实体件的话,实体件可能会超过传输的大小,还要考虑稳定性,所以我们需要分批传输那些偏大的实体件。
- 既然已经想好要分批传输的话那么就好考虑怎么储存你分批传输过来的值了,我刚开始想的是用最简单的方法用静态类来保持值,但是如果几个人同时传的话这个api就炸了,后面我就想生成一个txt文件来保持传输过来的值,这样的话只要名字不相同就不会有问题‘
- 想好要怎么给txt文件命名,最简单的是上传时间加上文件名称(你还可以加一些其他的文件信息比如:文件的大小,文件创建时间之类的来保障你创建的txt文件名不会重复),时间能有多精确就多精确,这样能保证txt文件名字不会重复
API代码
告诉各位一个调试api的方法:我是用的asp.net core Web应用程序,直接跑这个是显示一个页面,那么我们就可以在这个页面里面调用我们写好的api,直接在这个里面调用api是可以调试api代码的
// An highlighted block
{
public class ImageSaveController : ApiController
{
//传值要用的类
private class Data
{
//传输文件的二进制值
public byte[] file { get; set; }
//上传文件的文件夹名称
public string path { get; set; }
//文件名称
public string fileName { get; set; }
//写入文本的二进制值
public String filetxt { get; set; }
//判断是否是最后一批
public string wjon { get; set; }
//txt文本文件名
public string txtfile { get; set; }
}
//创建txt文件和分批传知道txt文件中
[HttpPost]
public string FileUp([FromBody] object bit)
{
string text = "";
//lock (_lock)
//{
string aaa = "";
try
{
text = "获取值";
string jsonstr = JsonConvert.SerializeObject(bit);
text = "转换值";
Data res = JsonConvert.DeserializeObject<Data>(jsonstr);
text = "拿webapi所在文件夹";
string txturl = AppDomain.CurrentDomain.BaseDirectory;
text = "新建临时文件夹";
CreatePath(txturl + "\\txt文件");
text = "判断txt文件是否存在";
// 判断文件是否存在,不存在则创建,否则读取值显示到txt文档
if (!System.IO.File.Exists(txturl + "\\txt文件" + "\\" + res.txtfile + ".txt"))
{
text = "创建写入txt文件";
FileStream fs1 = new FileStream(txturl + "\\txt文件" + "\\" + res.txtfile + ".txt", FileMode.Create, FileAccess.Write);//创建写入文件
text = "读取写入txt文件";
StreamWriter sw = new StreamWriter(fs1);
text = "开始写入txt文件";
sw.WriteLine(res.filetxt);//开始写入值
text = "关闭写入";
sw.Close();
fs1.Close();
}
else
{
text = "拿到写入txt文件";
FileStream fs = new FileStream(txturl + "\\txt文件" + "\\" + res.txtfile + ".txt", FileMode.Append, FileAccess.Write);
text = "读取写入txt文件";
StreamWriter sr = new StreamWriter(fs);
text = "开始写入txt文件";
sr.WriteLine(res.filetxt);//开始写入值
text = "关闭写入";
sr.Close();
fs.Close();
}
if (res.wjon == "yes")
{
text = "最后拿txt文件";
FileInfo fi = new FileInfo((txturl + "\\txt文件" + "\\" + res.txtfile + ".txt"));
text = "读取txt内容";
StreamReader sr = fi.OpenText();
String str = sr.ReadToEnd();
text = "关闭txt";
sr.Close();
text = "开始生成文件";
text = SaveFile(res.file, res.path, res.fileName, str);
text = "删除txt文件";
//File.Delete(txturl + "\\txt文件" + "\\" + res.txtfile + ".txt");
}
}
catch (Exception ex)
{
text += ex.Message;
aaa = "123";
}
if (aaa == "")
{
text = "ok";
}
return text;
}
//************************************************
//删除txt文件,因为我这个上传api的方法是一个人上传所以
//会把整个存放txt文件的文件夹删除掉,如果你们是要多人上
//传的话就删除对应的txt文件就好了
//*************************************************
[HttpPost]
public string DeleteTxt([FromBody] object bit)
{
string text = "";
try
{
text = "拿webapi所在文件夹";
string txturl = AppDomain.CurrentDomain.BaseDirectory;
text = "判断txt文件夹是否存在";
if (Directory.Exists(txturl + "\\txt文件"))
{
text = "读取文件夹";
DirectoryInfo din = new DirectoryInfo(txturl + "\\txt文件");
text = "读取文件夹文件";
FileInfo[] file = din.GetFiles();
text = "循环文件";
foreach (FileInfo fi in file)
{
text = "删除文件";
File.Delete(fi.FullName);
}
text = "删除文件夹";
Directory.Delete(txturl + "\\txt文件",true);
}
text = "ok";
}
catch (Exception ex)
{
text = ex.Message;
}
return text;
}
//生成文件
private string SaveFile(byte[] file, string path, string fileName, String filetxt)
{
string text = "";
try
{
text = "调用生成文件夹";
text = CreatePath(path);
text = "获取保存路径文件名";
var filename = fileName;
var filePath = string.Format(@"{0}\{1}", path, filename); //保存完整路径
var fullpath = filePath; //流写入路径
text = "进入创建文件using";
using (var ms = new MemoryStream())
{
text = "转换回二进制";
byte[] by = Convert.FromBase64String(filetxt);
text = "生成MemoryStream";
MemoryStream m = new MemoryStream(by);
text = "文件路径";
string files = string.Format(@"{0}", fullpath);
text = "生成文件";
FileStream fs = new FileStream(files, FileMode.Create);
m.WriteTo(fs);
text = "关闭项";
m.Close();
fs.Close();
m = null;
fs = null;
text = "ok";
return text;
}
}
catch (Exception ex)
{
return text += ":" + ex.Message;
}
}
/// <summary>
/// 创建文件路径
/// </summary>
/// <param name="filepath"></param>
/// <returns></returns>
private string CreatePath(string filepath)
{
string text = "";
text = "判断文件夹是否存在";
if (!Directory.Exists(filepath))
{
text = "生成文件夹";
Directory.CreateDirectory(filepath);
}
return text;
}
}
}
上传代码
// An highlighted block
DirectoryInfo din = new DirectoryInfo("文件夹路径");
FileInfo[] file = din.GetFiles();
int order = 0;
int or = 1;
string sqlgdh = "";
string urltxt = url;
foreach (FileInfo fi in file)
{
FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read); //将图片以文件流的形式进行保存
BinaryReader br = new BinaryReader(fs);
byte[] buffer = null;
try
{
buffer = br.ReadBytes((int)fs.Length); //将流读入到字节数组中
}
catch (Exception)
{
continue;
}
string result = "";
//文件是否大于10mb
if ((fi.Length / 1024 / 1024) > 10)
{
String source = "";
try
{
source = Convert.ToBase64String(buffer);
}
catch (Exception)
{
text = fi.FullName + ":文件过大!!内存不足无法上传!!" + System.Environment.NewLine;
continue;
}
int cishu = (int)Math.Ceiling(Convert.ToDouble(source.Length) / 1000000.00);
Application.DoEvents();
for (int s = 0; s < cishu; s++)
{
Application.DoEvents();
string wjon = "no";
String cftxt = "";
if ((s + 1) * 1000000 > source.Length)
{
cftxt = source.Substring(s * 1000000, source.Length - (s * 1000000));
wjon = "yes";
}
else
{
cftxt = source.Substring(s * 1000000, 1000000);
}
var requstData = new
{
path = urltxt,
fileName = fi.Name,
txtfile = fi.Name.Substring(0, fi.Name.Length - 4),
filetxt = cftxt,
wjon = wjon
};
result = HttpPostRequst(httpapi + "/ImageSave/FileUp", JsonConvert.SerializeObject(requstData));
if (!result.Contains("ok"))
{
break;
}
}
}
else
{
String source = Convert.ToBase64String(buffer);
var requstData = new
{
path = urltxt,
fileName = fi.Name,
txtfile = fi.Name.Substring(0, fi.Name.Length - 4),
filetxt = source,
wjon = "yes"
};
result = HttpPostRequst(httpapi + "/ImageSave/FileUp", JsonConvert.SerializeObject(requstData));
Application.DoEvents();
}
br.Close();
fs.Close();
if (!result.Contains("ok"))
{
text += fi.FullName + ":上传失败!!" + result + System.Environment.NewLine;
var requstData = new
{
wjon = "yes"
};
HttpPostRequst(httpapi + "/ImageSave/DeleteTxt", JsonConvert.SerializeObject(requstData));
}
Application.DoEvents();
}