在具有.net核心后端的angular 5应用程序中,用户可以上传其项目的附件。附件将发送到.NET Core后端,并保存在WebRootPath内的项目名称的文件夹中。
[HttpPost]
[Route("api/projects/{id}/upload")]
[Consumes("application/json", "application/json-patch+json", "multipart/form-data")]
public async Task<IActionResult> UploadAttachment(int id, FileForUpload upload)
{
var project = await _orbaAgent.GetDefaultProject(id);
if (upload != null && upload.File != null && upload.File.Length > 0)
{
var file = upload.File;
string serverPath = string.IsNullOrEmpty(_hostingEnvironment.WebRootPath) ? _hostingEnvironment.ContentRootPath : _hostingEnvironment.WebRootPath;
serverPath = Path.Combine(serverPath, "attachments", project.Code);
if (!Directory.Exists(serverPath))
{
Directory.CreateDirectory(serverPath);
}
using (var fileStream = new FileStream(Path.Combine(serverPath, file.FileName), FileMode.Create))
{
await file.CopyToAsync(fileStream);
}
}
return Ok();
}
附件的位置变为“ app / attachments /'projectcode”/。
要下载文件,我在html页面上使用了锚元素
<a href="attachments/{{defaultProject.code}}/{{att.name}}" download>{{att.name}}</a>
上传/下载效果完美。 唯一的问题是,当我部署新版本的应用程序时,app / attachment文件夹消失,所有附件也消失了。
有什么方法可以防止针对新版本删除此文件夹?还是如果不可能,我应该将文件保存在其他地方吗?