C# 如何更改Razor页面asp.net core上上载的图像
我尝试了StackOverflow的文件上传方法,并使用C# 如何更改Razor页面asp.net core上上载的图像,c#,asp.net-core,razor-pages,C#,Asp.net Core,Razor Pages,我尝试了StackOverflow的文件上传方法,并使用IHostEnvironment设置成功上传了图像。但是我搞不懂编辑模型。我想删除现有照片并在编辑表单中添加一张新照片 以下是模型: [Key] public int PostID { get; set; } [Required] [StringLength(160)] public string Title { get; set; } public string FeatureImage { get; s
IHostEnvironment
设置成功上传了图像。但是我搞不懂编辑模型。我想删除现有照片并在编辑表单中添加一张新照片
以下是模型:
[Key]
public int PostID { get; set; }
[Required]
[StringLength(160)]
public string Title { get; set; }
public string FeatureImage { get; set; }
以下是Create.cshtml.cs:
public class CreateModel : PageModel
{
private readonly RazorApp.Data.ApplicationDbContext _context;
private readonly IHostEnvironment hostingEnvironment;
public CreateModel(RazorApp.Data.ApplicationDbContext context, IHostEnvironment environment)
{
this.hostingEnvironment = environment;
_context = context;
}
[BindProperty]
public Post Post { get; set; }
[BindProperty]
public IFormFile Image { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (this.Image != null)
{
var fileName = GetUniqueName(this.Image.FileName);
var uploads = Path.Combine(hostingEnvironment.ContentRootPath, "wwwroot/uploads");
var filePath = Path.Combine(uploads, fileName);
this.Image.CopyTo(new FileStream(filePath, FileMode.Create));
this.Post.FeatureImage = fileName; // Set the file name
}
var emptyPost = new Post();
if (await TryUpdateModelAsync<Post>(
emptyPost,
"post",
p => p.Title, p => p.FeatureImage))
{
_context.Post.Add(Post);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
return Page();
}
private string GetUniqueName(string fileName)
{
fileName = Path.GetFileName(fileName);
return Path.GetFileNameWithoutExtension(fileName)
+ "_" + Guid.NewGuid().ToString().Substring(0, 4)
+ Path.GetExtension(fileName);
}
}
公共类CreateModel:PageModel
{
私有只读RazorApp.Data.ApplicationDbContext\u上下文;
私有只读IHostEnvironment主机环境;
公共CreateModel(RazorApp.Data.ApplicationDbContext上下文,IHostEnvironment环境)
{
this.hostingEnvironment=环境;
_上下文=上下文;
}
[BindProperty]
公共Post Post{get;set;}
[BindProperty]
公共文件映像{get;set;}
公共异步任务OnPostAsync()
{
如果(this.Image!=null)
{
var fileName=GetUniqueName(this.Image.fileName);
var uploads=Path.Combine(hostingEnvironment.ContentRootPath,“wwwroot/uploads”);
var filePath=Path.Combine(上传,文件名);
this.Image.CopyTo(新文件流(filePath,FileMode.Create));
this.Post.FeatureImage=fileName;//设置文件名
}
var emptyPost=new Post();
如果(等待TryUpdateModelAsync(
空邮,
“职位”,
p=>p.Title,p=>p.FeatureImage)
{
_context.Post.Add(Post);
wait_context.SaveChangesAsync();
返回页首(“/索引”);
}
返回页();
}
私有字符串GetUniqueName(字符串文件名)
{
fileName=Path.GetFileName(fileName);
返回Path.GetFileNameWithoutExtension(文件名)
+“389;”+Guid.NewGuid().ToString().Substring(0,4)
+GetExtension(文件名);
}
}
正如我所说,上传图像工作良好。但是我不知道edit.cshtml.cs是什么。如何删除现有照片并添加新图像?这是一个工作示例,您可以参考: Edit.cshtml
<div class="col-md-4">
<form method="post" enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Post.PostID" />
<div class="form-group">
<label asp-for="Post.Title" class="control-label"></label>
<input asp-for="Post.Title" class="form-control" />
<span asp-validation-for="Post.Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Post.FeatureImage" class="control-label"></label>
<input asp-for="Post.FeatureImage" class="form-control" />
<span asp-validation-for="Post.FeatureImage" class="text-danger"></span>
</div>
<div class="form-group">
<label class="control-label">New Image</label>
<input asp-for="Image" class="form-control" />
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
新形象
Edit.cshtml.cs
public class EditModel : PageModel
{
private readonly ChangeUploadedImage.Data.MyDbContext _context;
private readonly IHostingEnvironment hostingEnvironment;
public EditModel(MyDbContext context, IHostingEnvironment environment)
{
_context = context;
hostingEnvironment = environment;
}
[BindProperty]
public Post Post { get; set; }
[BindProperty]
public IFormFile Image { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
if (this.Image != null)
{
var path = Path.Combine(hostingEnvironment.ContentRootPath, "wwwroot/uploads", Post.FeatureImage);
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
var fileName = GetUniqueName(this.Image.FileName);
var uploads = Path.Combine(hostingEnvironment.ContentRootPath, "wwwroot/uploads");
var filePath = Path.Combine(uploads, fileName);
this.Image.CopyTo(new FileStream(filePath, FileMode.Create));
this.Post.FeatureImage = fileName;
}
_context.Attach(Post).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!PostExists(Post.PostID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToPage("./Index");
}
private string GetUniqueName(string fileName)
{
fileName = Path.GetFileName(fileName);
return Path.GetFileNameWithoutExtension(fileName)
+ "_" + Guid.NewGuid().ToString().Substring(0, 4)
+ Path.GetExtension(fileName);
}
}
公共类EditModel:PageModel
{
私有只读更改上载edimage.Data.MyDbContext\u context;
私有只读IHostingEnvironment主机环境;
公共编辑模型(MyDbContext上下文,IHostingEnvironment环境)
{
_上下文=上下文;
主机环境=环境;
}
[BindProperty]
公共Post Post{get;set;}
[BindProperty]
公共文件映像{get;set;}
公共异步任务OnPostAsync()
{
如果(!ModelState.IsValid)
{
返回页();
}
如果(this.Image!=null)
{
var path=path.Combine(hostingEnvironment.ContentRootPath,“wwwroot/uploads”,Post.FeatureImage);
if(System.IO.File.Exists(path))
{
System.IO.File.Delete(路径);
}
var fileName=GetUniqueName(this.Image.fileName);
var uploads=Path.Combine(hostingEnvironment.ContentRootPath,“wwwroot/uploads”);
var filePath=Path.Combine(上传,文件名);
this.Image.CopyTo(新文件流(filePath,FileMode.Create));
this.Post.FeatureImage=文件名;
}
_context.Attach(Post.State=EntityState.Modified;
尝试
{
wait_context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
如果(!PostExists(Post.PostID))
{
返回NotFound();
}
其他的
{
投掷;
}
}
返回页首(“/索引”);
}
私有字符串GetUniqueName(字符串文件名)
{
fileName=Path.GetFileName(fileName);
返回Path.GetFileNameWithoutExtension(文件名)
+“389;”+Guid.NewGuid().ToString().Substring(0,4)
+GetExtension(文件名);
}
}
谢谢!我忘了在cshtml文件中添加多部分/表单数据。我还使用了var postToUpdate=wait_context.Post.FindAsync(id);这就是我无法编辑的原因。非常感谢你。