简体   繁体   中英

Write and Delete Files in Web Root Folder When Using FTP in ASP.NET Core

I'm making a web shop using ASP.NET Core Razor Pages and as a part of this I want to perform CRUD operations for product images.

My Admin page for writing image files looks like this:

    public IFormFile Thumbnail { get; set; }

    public AddModel(IWebHostEnvironment webHostEnvironment)
    {
        _webHostEnvironment = webHostEnvironment;
    }

    public void WriteImage(string reference)
    {
        string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "images", "products", Product.Name.ToLower());

        if (!Directory.Exists(uploadsFolder))
        {
            Directory.CreateDirectory(uploadsFolder);
        }

        string thumbnailPath = $"{Product.Name.ToLower()}-{reference}-thumbnail.png";

        string fileLocation = Path.Combine(uploadsFolder, thumbnailPath);

        using (var fileStream = new FileStream(fileLocation, FileMode.Create))
        {
            Thumbnail.CopyTo(fileStream);
        }
    }

Everything works in development. Though I suspect things will be different once the site goes live. I'm planning to publish the website with FTP using FileZilla, and don't I need to log in to the FTP server in order to write and delete files located in the Web Root Path?

You cannot just upload your app to any FTP server, you also need to be able to run your app somehow. See Host and deploy ASP.NET Core for further information. Your app will run as certain user, and as such does not need to logon again to access the server's file system, but it depends on the server to which directories your app will have access to.

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