【发布时间】:2021-12-24 12:43:50
【问题描述】:
经过几天的谷歌搜索和尝试,我无法将 xsd 文件上传到网络服务器。
这里是创建表单:
<form asp-action="Create" enctype="multipart/form-data" method="post">
<input asp-for="catname" class="form-control" />
<input type="file" asp-for="xsddata" class="form-control" />
<input type="submit" value="Create" class="btn btn-primary" />
</form>
模型如下所示:
public class cat{
[Key]
public int catid { get; set; }
public string catname { get; set; }
public string xsddata { get; set; }}
我只想将路径和文件名保存在数据库中。
在控制器类中我添加了保存方法:
[HttpPost]
public async Task<IActionResult> Upload([FromForm]IFormFile file)
{
var path = Startup.myAppPath();
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
FileInfo fileInfo = new FileInfo(file.FileName);
var fullPath = Path.Combine(path, fileInfo.Name);
if (!System.IO.File.Exists(fullPath))
{
using (FileStream fileStream = new FileStream(fullPath, FileMode.Create))
{
await file.CopyToAsync(fileStream);
}
return new JsonResult(new { FileName = fileInfo.Name });
}
return BadRequest();
}
但是现在,即使在我选择了一个文件并且在我点击“提交”按钮之前,IDE 也会崩溃。
这让我疯狂了好几天,我找不到可以依赖的例子。 如果你们能帮帮我就好了。
【问题讨论】:
标签: c# file upload asp.net-core-mvc