Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法删除文件,因为另一个进程ASP.NET Core MVC正在使用该文件_Javascript_C#_Html_Asp.net_Asp.net Core - Fatal编程技术网

Javascript 无法删除文件,因为另一个进程ASP.NET Core MVC正在使用该文件

Javascript 无法删除文件,因为另一个进程ASP.NET Core MVC正在使用该文件,javascript,c#,html,asp.net,asp.net-core,Javascript,C#,Html,Asp.net,Asp.net Core,我正在使用ASP.NETCore和MVC创建一个应用程序。我目前正在使用visual studio和IIS express。 以下是我目前的项目结构: *项目目录 -wwwroot -区域 -附件 -控制器 -模型 -观点 我当前将图像存储在附件文件夹中 以前我在startup.cs中写过类似的东西 app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvi

我正在使用ASP.NETCore和MVC创建一个应用程序。我目前正在使用visual studio和IIS express。 以下是我目前的项目结构:

*项目目录

-wwwroot

-区域

-附件

-控制器

-模型

-观点

我当前将图像存储在附件文件夹中

以前我在startup.cs中写过类似的东西

app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(), "Attachments")),
            RequestPath = "/Attachments"
        });
我也做了如下类似的事情:

appendImage(@Url.Content("~/Attachments/")+result.fileName);
我这样做是为了在视图上显示图像。图像显示成功

我现在试图实现的是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";
  }
}
如果(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);
}
[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 "";

 }
下面调用该函数:

$.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) {

                }
        });
调用appendImage()后;我更改
标记的href。当用户单击链接时,用户将被定向到另一个页面(edit.cshtml)。在页面中,驻留在该路径中的图像也会显示如下代码:

在这个新页面(edit.cshtml)中,有一个删除按钮。单击delete(删除)按钮后,程序的执行转到控制器,该控制器是该控制器的功能:

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);
}
[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 "";

 }
编辑以回答问题:

附加

System.GC.Collect(); 
System.GC.WaitForPendingFinalizers(); 
在system.io.file.delete之前

可以用给定的代码替换C#方法DeleteOneItem。也许它会起作用

try
{
 System.GC.Collect(); 
 System.GC.WaitForPendingFinalizers(); 
 System.IO.File.Delete(fullImagePath);
}
catch(Exception e){
}
[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 "";
}

代码中是否也显示(使用)了要删除的图像?如果是这样的话,我猜您需要从使用它的任何对象“释放”这个文件,然后才能删除它。行
appendImage(@Url.Content(“~/Attachments/”)+result.fileName)…似乎正在“使用”该文件。@JohnG,那段代码appendImage();在我的视图(.cshtml)中调用。删除文件(system.io.file.delete)的代码段驻留在控制器中。如何释放该文件?在调用system.io.file.delete之前,我必须在控制器内部编写一些其他代码?这取决于appendImage()在做什么。能否在appendImage()中共享相关代码?可能有一些文件I/O逻辑在需要时没有释放/关闭/处理流。@AashishKoirala,我编辑了这个问题以包含更多细节。您看过其中的任何一个吗?