我试图让下面的代码正常工作,但是我一直遇到Microsoft.Web.Helpers v3.2.6和我当前的NETCore 2.1软件包的兼容性问题。而且,对于我的生活,我无法得到最简单的IsPost调用和请求被认可。我肯定这是个明显的解决办法,但我找不到!
提前谢谢你的指示..。
@using Microsoft.Web.Helpers;
@{
var fileName = "";
if (IsPost) {
var fileSavePath = "";
var uploadedFile = Request.Files[0];
fileName = Path.GetFileName(uploadedFile.FileName);
fileSavePath = Server.MapPath("~/App_Data/UploadedFiles/" +
fileName);
uploadedFile.SaveAs(fileSavePath);
}
}
<!DOCTYPE html>
<html>
<head>
<title>FileUpload - Single-File Example</title>
</head>
<body>
<h1>FileUpload - Single-File Example</h1>
@FileUpload.GetHtml(
initialNumberOfFiles:1,
allowMoreFilesToBeAdded:false,
includeFormTag:true,
uploadText:"Upload")
@if (IsPost) {
<span>File uploaded!</span><br/>
}
</body>
</html>发布于 2018-11-15 08:31:22
WebHelpers库与ASP.NET核心不兼容。它依赖于System.Web,而.NET核心一直被设计为远离它。
IsPost块的替代品是一个处理方法。按照惯例,如果用于请求页面的方法是OnPost (这是IsPost属性用于检查的),则将执行名为IsPost的处理程序方法。
就我个人而言,我从来不理解FileUpload助手的意义,除非您希望允许用户向页面添加额外的文件上传(在本例中显然不是这样)。input type="file"更容易添加到页面中。
ASP.NET核心中的文件上传与网页完全不同。下面是一些关于它的指导:https://www.learnrazorpages.com/razor-pages/forms/file-upload
https://stackoverflow.com/questions/53308139
复制相似问题