实现代码
//xml文件
<?xml version="1.0" encoding="utf-8" ?>
<updateList>
<url>http://localhost:5000/api/Update/</url>
<files>
<file name="1.dll" version="1.0"></file>
<file name="1.dll" version="1.1"></file>
<file name="AutoUpdate.Test.exe" version="1.1"></file>
</files>
</updateList>
//Model
public class UpdateModel {
public string name { get; set; }
public string version { get; set; }
}
public class UpdateModel_Out {
public string url { get; set; }
public List<UpdateModel> updateList { get; set; }
}
//控制器
namespace AutoUpdate.WebApi.Controllers {
[Route("api/[controller]/[Action]")]
[ApiController]
public class UpdateController : ControllerBase {
[HttpGet]
public JsonResult Index() {
return new JsonResult(new { code = 10, msg = "success" });
}
[HttpPost]
public JsonResult GetUpdateFiles([FromBody] List<UpdateModel> input) {
string xmlPath = AppContext.BaseDirectory "UpdateList.xml";
XDocument xdoc = XDocument.Load(xmlPath);
var files = from f in xdoc.Root.Element("files").Elements() select new { name = f.Attribute("name").Value, version = f.Attribute("version").Value };
var url = xdoc.Root.Element("url").Value;
List<UpdateModel> updateList = new List<UpdateModel>();
foreach(var file in files) {
UpdateModel model = input.Find(s => s.name == file.name);
if(model == null || file.version.CompareTo(model.version) > 0) {
updateList.Add(new UpdateModel {
name = file.name,
version = file.version
});
}
}
UpdateModel_Out output = new UpdateModel_Out {
url = url,
updateList = updateList
};
return new JsonResult(output);
}
[HttpPost]
public FileStreamResult DownloadFile([FromBody] UpdateModel input) {
string path = AppContext.BaseDirectory "files\" input.name;
FileStream fileStream = new FileStream(path, FileMode.Open);
return new FileStreamResult(fileStream, "application/octet-stream");
}
}
}