Myvs文件夹结构看起来如下:
-RLLPDocumentUpload
-Controllers
DocumentController.cs
HomeController.cs
-Models
Document.cs
-Pages
UploadDocumentModel.cs
-Views
-Document
Upload.cshtml
Program.cs
Startup.cs
DocumentController.cs
using Microsoft.AspNetCore.Mvc;
using RLPDocumentUpload.Models;
using Microsoft.AspNetCore.Http;
using System.IO;
using System.Threading.Tasks;
namespace RLPDocumentUpload.Controllers
{
public class DocumentController : Controller
{
[HttpGet]
public IActionResult Upload()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Upload(Document document, IFormFile upload)
{
if (!ModelState.IsValid)
{
return View(document);
}
if (upload != null)
{
var filePath = Path.Combine("RLPDocumentUpload/Uploads", upload.FileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await upload.CopyToAsync(stream);
}
document.FilePath = filePath;
ViewBag.SuccessMessage = "Upload Successful";
}
// Handle other logic here
return View(document);
}
}
}
Document.cs
namespace RLPDocumentUpload.Models
{
public class Document
{
public int Id { get; set; }
public DateTime DocumentDate { get; set; }
public string DocumentDesc { get; set; } = string.Empty;
public string DocumentSource { get; set; } = string.Empty;
public string DocumentType { get; set; } = string.Empty;
public string FilePath { get; set; } = string.Empty;
}
}
uploaddocumentModel.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Http;
using System.IO;
using System.Threading.Tasks;
using RLPDocumentUpload.Models;
namespace RLPDocumentUpload.Pages
{
public class UploadDocumentModel : PageModel
{
[BindProperty]
public Document Document { get; set; } = new Document();
[BindProperty]
public IFormFile? Upload { get; set; }
public string SuccessMessage { get; set; } = string.Empty;
public void OnGet()
{
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
if (Upload != null)
{
var filePath = Path.Combine("RLPDocumentUpload/Uploads", Upload.FileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await Upload.CopyToAsync(stream);
}
Document.FilePath = filePath;
SuccessMessage = "Upload Successful";
}
// Handle other logic here
return Page();
}
}
}
upload.cshtml
<h2>Upload Document</h2>
<form asp-action="Upload" method="post" enctype="multipart/form-data">
<div class="form-group">
<label asp-for="DocumentDesc"></label>
<input asp-for="DocumentDesc" class="form-control" />
<span asp-validation-for="DocumentDesc" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DocumentSource"></label>
<input asp-for="DocumentSource" class="form-control" />
<span asp-validation-for="DocumentSource" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DocumentType"></label>
<input asp-for="DocumentType" class="form-control" />
<span asp-validation-for="DocumentType" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DocumentDate"></label>
<input asp-for="DocumentDate" type="date" class="form-control" />
<span asp-validation-for="DocumentDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="FilePath"></label>
<input asp-for="FilePath" type="file" class="form-control" />
<span asp-validation-for="FilePath" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Upload</button>
</form>
@if (!string.IsNullOrEmpty(ViewBag.SuccessMessage))
{
<div id="successPopup" style="display:none;">
<p>@ViewBag.SuccessMessage</p>
<button onclick="closePopup()">Close</button>
</div>
<script>
function showPopup() {
document.getElementById('successPopup').style.display = 'block';
}
function closePopup() {
document.getElementById('successPopup').style.display = 'none';
}
// Show the popup when the page loads
window.onload = function() {
showPopup();
};
</script>
}
copilot一直在圈子里四处走动。 任何可以提供的帮助将不胜感激。 谢谢, 马特·佩斯利
您不需要“ uploadDocumentModel.cs”,即“ razorpages”模式。提交应将文件发布给控制器。根据控制器端点
public async Task<IActionResult> Upload(Document document, IFormFile upload)
Upload.cshtml