简体   繁体   English

ASP.NET Core使用FTP时写入和删除Web根目录下的文件

[英]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.我正在使用 ASP.NET 核心 Razor 页面创建一个 web 商店,作为其中的一部分,我想对产品图像执行 CRUD 操作。

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?我打算使用 FileZilla 发布 FTP 的网站,我是否需要登录到 FTP 服务器才能写入和删除位于 Web 根路径中的文件?

You cannot just upload your app to any FTP server, you also need to be able to run your app somehow.您不能只将您的应用程序上传到任何 FTP 服务器,您还需要能够以某种方式运行您的应用程序。 See Host and deploy ASP.NET Core for further information.有关详细信息,请参阅 托管和部署 ASP.NET 核心 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.您的应用程序将以特定用户身份运行,因此无需再次登录即可访问服务器的文件系统,但这取决于您的应用程序可以访问哪些目录的服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .css 根文件夹中的文件不会加载到 ASP.NET Core MVC web 应用程序中的 _Layout 中 - .css files in root folder don't load in _Layout in ASP.NET Core MVC web app ASP.NET Core 5.0 Web API 在尝试使用 HttpDelete 删除记录时抛出错误 405 - ASP.NET Core 5.0 Web API throws error 405 when trying to delete record using HttpDelete 使用 ASP.NET Core MVC 将带有文件的数据发布到 api (ASP.NET Core Web API) - Post data with files using ASP.NET Core MVC to api (ASP.NET Core Web API) 使用 ASP.net 内核 web api 发送多个文件 - Send multiple files using ASP.net core web api Stream 文件从 FTP 服务器使用 FluentFTP 到 web 客户端在 Z9E0DA8438E1E38A18DDZCE30 - Stream file from FTP server using FluentFTP to web client in ASP.NET Core “wwwroot”文件夹中的某些文件未在 ASP.NET 核心 web 部署中发布 - Some files in "wwwroot" folder are not published in ASP.NET Core web deploy ASP.net获取不包括根文件夹的文件 - ASP.net get files excluding root folder ASP.NET Core 5 API 写文件到 Azure ZC6E190B284633C48E39E55049 磁盘空间 - ASP.NET Core 5 API write files on Azure Web App disk space 在 asp.net 内核中使用 .resx 文件 - Using .resx files in asp.net core 将文件从 ASP.NET Core web api 发布到另一个 ASP.NET Core web api - Post files from ASP.NET Core web api to another ASP.NET Core web api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM