如何解决更新图像错误消息因为它正在被另一个进程使用
我有一个功能可以在asp.net核心应用程序中编辑Book
及其图像,但是当我编辑该书时,出现此错误:
IOException:进程无法访问文件 'C:\ Users \ Mostafa \ source \ repos \ Library \ Library \ wwwroot \ uploads \ image.jpg' 因为它正在被另一个进程使用。
这是我的控制器中的编辑功能:
[HttpPost]
public IActionResult Update(Book book,BookViewModel bookViewModel)
{
string fileName = string.Empty;
if (bookViewModel.File != null)
{
string uploads = Path.Combine(hosting.WebRootPath,"uploads");
fileName = Guid.NewGuid() + bookViewModel.File.FileName;
string fullPath = Path.Combine(uploads,fileName);
string oldfileName = _bookRepository.FindBook(book.Bookid).imageURL;
string fulloldpath = Path.Combine(uploads,oldfileName);
if(fullPath != fulloldpath)
{
System.IO.File.Delete(fulloldpath);
bookViewModel.File.CopyTo(new FileStream(fullPath,FileMode.Create));
bookViewModel.Book.imageURL = fileName;
}
}
if (!ModelState.IsValid)
{
bookViewModel.Authors = _authorRepository.GetAll();
return View(bookViewModel);
}
_bookRepository.Update(bookViewModel.Book);
return RedirectToAction("List");
}
这是FindBook
方法:
public Book FindBook(int id)
{
var book = _context.Books.Include(a => a.Author).SingleOrDefault(b => b.Bookid == id);
return book;
}
这是编辑方法:
public void Update(Book newBook)
{
_context.Update(newBook);
_context.SaveChanges();
}
这是我的剃须刀页面:
<div class="col-md-6">
<div class="form-group" style="padding-left:40%;padding-top:15%;">
<div class="col-sm-4">
<img src="~/uploads/@Model.Book.imageURL" style="margin:10px" height="200" width="200" id="imagebookpre" />
<input type="file" asp-for="File" class="form-control" onchange="ShowImagePreview(this,document.getElementById('imagebookpre'))" />
<span asp-validation-for="File" class="text-danger"></span>
</div>
</div>
</div>
请问有人可以帮我吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。