简体   繁体   中英

How to delete a file from Azure Web App local storage?

This would be the obvious place to start: Delete and upload files into Azure webapp local storage programatically . But it was asked 3 years ago, and like that OP, I can't make heads or tails of any of the answers.

I understand that Kudu has an API to handle this stuff. I can see the file in the Kudu web browser (https:// mysitename .scm.azurewebsites.net/DebugConsole).

But, surely it must be easier than having to call an API just to delete a file?!?!? Come on! Really?

在此处输入图片说明

This file was easily uploaded using HttpPostedFileBase.SaveAs(path) . But HttpPostedFileBase offeres no Delete option.

I've tried just good ol System.IO.File . Figured if it was called from the context of the app's server it would work:

    public void DeleteFile(string fileToDelete)
    {
        try
        {
            if (System.IO.File.Exists(fileToDelete))
                System.IO.File.Delete(fileToDelete);
        }
        catch (Exception ex)
        {
            Logging.LogError(....);
        }
    }

Using this, nothing happens. No error is logged. No exception is thrown. Nothing. It acts all happy as if it worked, but the file remains.

Surely someone has dealt with this already? There must be a simple solution.

If you're trying to delete the file within the web app itself, it would be easy to do that.

As per the code public void DeleteFile(string fileToDelete) you provide, the main issue may due to the parameter fileToDelete is not a correct path. And you just use File.Exists(fileToDelete) to check if the file exists or not, but if the file does not exist, the code does nothing. So the scenario occurs: "Using this, nothing happens. No error is logged. No exception is thrown".

You can add an else block which do something when the file does not exist, or you can directly use a absolute path for the file deletion like below:

string fileToDelete = "D:\home\site\wwwroot\" + "uploads\xxx.zip"

then pass the fileToDelete to the method public void DeleteFile(string fileToDelete)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM