在 Web API 服务器文件系统中保存一个文件

Save a file in Web API server file system

我正在使用 .NET Framework 4.5.1 和 C# 开发 ASP.NET Web API 2.2。

我想将文件保存在服务器的文件夹中(该文件夹位于 Web API 与 运行 相同的服务器中)。

这是我的代码:

public class ProductionOrdersController : ApiController
{
    private readonly IGenericRepository<PRODUCTION_ORDERS> m_PORepo;
    private readonly IGenericRepository<AGGREGATIONS> m_AggRepo;
    private readonly IGenericRepository<AGGREGATION_CHILDS> m_AggChRepo;

    public ProductionOrdersController(
        IGenericRepository<PRODUCTION_ORDERS> repository,
        IGenericRepository<AGGREGATIONS> aggRepo,
        IGenericRepository<AGGREGATION_CHILDS> aggChRepo)
    {
        m_PORepo = repository;
        m_AggRepo = aggRepo;
        m_AggChRepo = aggChRepo;
    }

    [HttpPut]
    public HttpResponseMessage Close(long orderNumber)
    {
        HttpResponseMessage response = null;

        PRODUCTION_ORDERS po = m_PORepo.GetById(orderNumber);

        if (po != null)
        {
            // Generate XML
            ProductionOrderReport poReport =
                new ProductionOrderReport(m_PORepo, m_AggRepo, m_AggChRepo);

            XDocument doc = poReport.GenerateXMLReport(orderNumber);
            // Save XML.
        }

        return response;
    }
}

我的问题是我不知道该怎么做。我在网上找的都是关于如何上传文件的。

我该怎么做?

尝试 Save XDocument

的方法
doc.Save("<path where you want to store>");

确保您的应用程序有权访问您尝试存储文件的文件夹。

查找更多信息here