Can I write a file to a folder on a server machine from a Web API app running on it?

前端 未结 2 1332
失恋的感觉
失恋的感觉 2021-02-14 14:59

I have this code in my Web API app to write to a CSV file:

private void SaveToCSV(InventoryItem invItem, string dbContext)
{
    string csvHeader = \"id,pack_siz         


        
相关标签:
2条回答
  • 2021-02-14 15:31

    Thanks to Patrick Hofman; this is the exact code I am using, and it is saved to the project's App_Data folder:

    public static string appDataFolder = HttpContext.Current.Server.MapPath("~/App_Data/");
    . . .
    string csvFilename = string.Format("Platypus{0}.csv", dbContextAsInt);
    string fullSavePath = string.Format("{0}{1}", appDataFolder, csvFilename);
    
    0 讨论(0)
  • 2021-02-14 15:35

    In the case of the IIS folder the application has rights to write in there. I suggest to write files to the App_Data folder.

    When you want to save files outside the IIS application folder you have to give the service account of the IIS application pool (I think it by default is NETWORKSERVICE) the appropriate rights on that folder.

    As requested by B. Clay Shannon my implementation:

    string fullSavePath = HttpContext.Current.Server.MapPath(string.Format("~/App_Data/Platypus{0}.csv", dbContextAsInt));
    
    0 讨论(0)
提交回复
热议问题