请求
var FileString=HttpHelper.HttpApi(url, null, "post");
var FileName = "1.txt";
#region 本地存文件地址
string fullPath = Path.Combine(Directory.GetCurrentDirectory(), "File");
if (!Directory.Exists(fullPath))
{
Directory.CreateDirectory(fullPath);
}
var newfile = "File/" + FileName;
var newfullPath = Path.Combine(Directory.GetCurrentDirectory(), newfile);
if (File.Exists(Path.GetFullPath(newfullPath)))
{
File.Delete(Path.GetFullPath(newfullPath));
}
BinaryToFile(newfullPath, FileString);
#endregion
public void BinaryToFile(string path, string binary)
{
System.IO.FileStream fs = new(path, FileMode.Create, FileAccess.Write);
BinaryWriter bw = new(fs);
bw.Write(Convert.FromBase64String(binary));
bw.Close();
fs.Close();
}
接收
[HttpPost]
public async Task<JsonResult> GetFile()
{
var result = string.Empty;
try
{
var FilePath= Path.Combine(Directory.GetCurrentDirectory(), @"File\1.txt");
var fs = new FileStream(FilePath, FileMode.Open);
int fileLength = Convert.ToInt32(fs.Length);
byte[] fileByteArray = new byte[fileLength];
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
for (int i = 0; i < fileLength; i++)
{
br.Read(fileByteArray, 0, fileLength);
}
result = Convert.ToBase64String(fileByteArray);
br.Close();
fs.Close();
}
catch
{
}
return Json(result);
}