jin*_*teo 3 html javascript c# asp.net asp.net-core
我正在使用 ASP.Net Core 和 MVC 来创建一个应用程序。我目前正在使用 Visual Studio 和 IIS express。以下是我目前的项目结构:
*项目目录
-wwwroot
-区域
-附件
-控制器
-楷模
- 意见
我目前将图像存储在附件文件夹中。
以前我在我的startup.cs里面写过类似的东西
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "Attachments")),
RequestPath = "/Attachments"
});
Run Code Online (Sandbox Code Playgroud)
我也在下面做了这样的事情:
appendImage(@Url.Content("~/Attachments/")+result.fileName);
Run Code Online (Sandbox Code Playgroud)
我这样做是为了在我的视图中显示图像。图像显示成功。
我现在想要实现的是在 UI 上允许用户选择删除该附件文件夹中的文件
我尝试了以下代码:
string contentRootPath = _hostingEnvironment.ContentRootPath;
string fullImagePath = Path.Combine(contentRootPath + "\\Attachments", currentItemToDelete.FileName);
if (System.IO.File.Exists(fullImagePath))
{
try{
System.IO.File.Delete(fullImagePath);
}catch(Exception e){
operationResult = "Attachment Path. Internal Server Error";
}
}
Run Code Online (Sandbox Code Playgroud)
执行确实进入 ,if (System.IO.File.Exists(fullImagePath))
但在到达 时引发异常System.IO.File.Delete
。该异常表明驻留在该路径中的文件正被另一个进程使用。因此我无法删除该文件。访问该文件的唯一进程是我同时创建/调试的 Web 应用程序。如何防止发生此异常?我是否必须使用其他类型的代码来删除文件?
编辑以包含更多详细信息:
在我的视图中(index.cshtml):
appendImage 是一个 javascript 函数:
function appendImage(imgSrc) {
var imgElement = document.createElement("img");
imgElement.setAttribute('src', imgSrc);
if (imgSrc.includes(null)) {
imgElement.setAttribute('alt', '');
}
imgElement.setAttribute('id', "img-id");
var imgdiv = document.getElementById("div-for-image");
imgdiv.appendChild(imgElement);
}
Run Code Online (Sandbox Code Playgroud)
该函数在下面调用:
$.ajax({
url:'@Url.Action("GetDataForOneItem", "Item")',
type: "GET",
data: { id: rowData.id },
success: function (result) {
removeImage();
appendImage(@Url.Content("~/Attachments/")+result.fileName);
$("#edit-btn").attr("href", '/Item/EditItem?id=' + result.id);
},
error: function (xhr, status, error) {
}
});
Run Code Online (Sandbox Code Playgroud)
调用 appendImage(); 后 我更改了<a>
标签的 href 。当用户点击链接时,用户被定向到另一个页面(edit.cshtml)。在页面中,驻留在该路径中的图像也以如下代码显示:
<img src="@Url.Content("~/Attachments/"+Model.FileName)" alt="item image" />
在这个新页面(edit.cshtml)中,有一个删除按钮。单击删除按钮后,程序的执行将转到控制器,即此控制器功能:
[HttpPost]
public string DeleteOneItem(int id)
{
//query the database to check if there is image for this item.
var currentItemToDelete = GetItemFromDBDateFormatted(id);
if (!string.IsNullOrEmpty(currentItemToDelete.FileName))
{
//delete the image from disk.
string contentRootPath = _hostingEnvironment.ContentRootPath;
string fullImagePath = Path.Combine(contentRootPath + "\\Attachments", currentItemToDelete.FileName);
if (System.IO.File.Exists(fullImagePath))
{
try
{
System.IO.File.Delete(fullImagePath);
}catch(Exception e)
{
}
}
}
return "";
}
Run Code Online (Sandbox Code Playgroud)
编辑回答问题:
加入
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
Run Code Online (Sandbox Code Playgroud)
在 system.io.file.delete 之前
小智 6
你可以用这个给定的代码替换你的 C# 方法 DeleteOneItem。也许它可能会起作用。
[HttpPost]
public string DeleteOneItem(int id)
{
//query the database to check if there is image for this item.
var currentItemToDelete = GetItemFromDBDateFormatted(id);
if (!string.IsNullOrEmpty(currentItemToDelete.FileName))
{
//delete the image from disk.
string contentRootPath = _hostingEnvironment.ContentRootPath;
string fullImagePath = Path.Combine(contentRootPath + "\\Attachments", currentItemToDelete.FileName);
if (System.IO.File.Exists(fullImagePath))
{
try
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.IO.File.Delete(fullImagePath);
}
catch (Exception e) { }
}
}
return "";
}
Run Code Online (Sandbox Code Playgroud)
try
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.IO.File.Delete(fullImagePath);
}
catch(Exception e){
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2734 次 |
最近记录: |