更新都寫完了,但是要更新文件要怎么操作呢?連接服務(wù)器然后上傳上去,修改下xml的版本號(hào)當(dāng)然也是可以的,但是還是沒有寫個(gè)程序使用起來方便,所以這一篇就寫一個(gè)簡(jiǎn)單的上傳程序。后臺(tái)依然是使用的.net core webapi,然后新建個(gè)控制器來接收上傳來的文件。前臺(tái)就直接使用layui隨便搭個(gè)上傳表單。
開發(fā)環(huán)境:.NET Core 3.1
開發(fā)工具:Visual Studio 2019
實(shí)現(xiàn)代碼:
namespace Autoupdate.WebApi.Controllers {
[Route("api/[controller]")]
[ApiController]
public class UploadController : ControllerBase {
private static object obj = new object();
[HttpPost]
public JsonResult Upload() {
IFormFileCollection formFile = Request.Form.Files;
if(formFile.Count == 0) {
throw new Exception("沒有文件");
}
ReturnResult result = new ReturnResult(Result.success);
string path = AppContext.BaseDirectory + "files\\";
string xmlPath = AppContext.BaseDirectory + "updateList.xml";
try {
foreach(var file in formFile) {
string fileName = path + file.FileName;
using FileStream fileStream = System.IO.File.create(fileName);
file.CopyTo(fileStream);
fileStream.Flush();
}
List<string> updateFiles = formFile.select(s => s.FileName).ToList();
updateXml(updateFiles);
}
catch(Exception ex) {
result.result = Result.error;
result.msg = ex.Message;
}
return new JsonResult(result);
}
private void updateXml(List<string> updateFiles) {
lock(obj) {
string xmlPath = AppContext.BaseDirectory + "updateList.xml";
XDocument xdoc = XDocument.Load(xmlPath);
foreach(string file in updateFiles) {
var ele_files = xdoc.Root.Element("files");
XElement xele = ele_files.Elements().FirstOrDefault(s => s.Attribute("name").Value == file);
if(xele != null) {
string oldVer = xele.Attribute("version").Value;
xele.SetAttributeValue("version", Math.Round(decimal.Parse(oldVer) + (decimal)0.01, 2));
}
else {
XElement addXele = new XElement("file");
addXele.SetAttributeValue("name", file);
addXele.SetAttributeValue("version", "1.00");
ele_files.Add(addXele);
}
}
xdoc.Save(xmlPath);
}
}
}
}
//html代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>文件更新</title>
<link rel="stylesheet" href="./layui/css/layui.css">
</head>
<body>
<div class="layui-form-item" style="margin:15%">
<div class="layui-col-md10 layui-col-xs9 layui-form-item-mt5">
<label class="layui-form-label">文件上傳</label>
<div class="layui-input-block">
<input type="text" id="filename" name="filename" class="layui-input" readonly />
</div>
</div>
<div class="layui-col-md2 layui-col-xs1 layui-form-item-mt5">
<button class="layui-btn" id="btnBrowse">瀏覽</button>
<button class="layui-btn" id="submit" lay-filter="submit" lay-submit>上傳</button>
</div>
</div>
<script src="./layui/layui.js"></script>
<script>
var layer = layui.layer, form = layui.form, $ = layui.jquery,
upload = layui.upload;
form.render();
upload.render({
elem: '#btnBrowse'
, url: 'http://localhost:5000/api/Upload'
, headers: {}
, data: {}
, accept: 'file'
, multiple: true
, auto: false
, bindAction: '#submit'
, choose: function (obj) {
$("#filename").val("");
obj.preview(function (index, file, result) {
$("#filename").val($("#filename").val() + file.name + " ; ");
})
}
, done: function (res, index, upload) {
layer.closeAll('loading');
layer.msg(res.msg);
},
error: function (res) {
layer.closeAll('loading');
}
})
</script>
</body>
</html>
實(shí)現(xiàn)效果:
代碼解析:文件上傳完之后,若xml文件中已經(jīng)存在該文件名稱,則進(jìn)行版本+0.01,否則新加一條節(jié)點(diǎn)信息,默認(rèn)版本號(hào)為1.00。然后這里由于要操作文件,所以又加了個(gè)鎖來防止占用(總覺得這個(gè)方式不太合適),主要是因?yàn)閘ayui的多文件上傳是循環(huán)調(diào)接口實(shí)現(xiàn)的,感覺不太好,建議使用其他框架或者對(duì)js代碼進(jìn)行更改。
該文章在 2023/2/27 10:20:33 編輯過