我可以写一个文件到一个文件夹服务器计算机上从上运行的Web应用程序的API? [英] Can I write a file to a folder on a server machine from a Web API app running on it?

查看:153
本文介绍了我可以写一个文件到一个文件夹服务器计算机上从上运行的Web应用程序的API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code在我的Web API的应用程序写入到CSV文件:

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_size,description,vendor_id,department,subdepartment,unit_cost,unit_list,open_qty,UPC_code,UPC_pack_size,vendor_item,crv_id";

    int dbContextAsInt = 0;
    int.TryParse(dbContext, out dbContextAsInt);
    string csvFilename = string.Format("Platypus{0}.csv", dbContextAsInt);

    string csv = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12}", invItem.ID,
        invItem.pksize, invItem.Description, invItem.vendor_id, invItem.dept, invItem.subdept, invItem.UnitCost,
        invItem.UnitList, invItem.OpenQty, invItem.UPC, invItem.upc_pack_size, invItem.vendor_item, invItem.crv_id);

    string existingContents;
    using (StreamReader sr = new StreamReader(csvFilename))
    {
        existingContents = sr.ReadToEnd();
    }

    using (StreamWriter writetext = File.AppendText(csvFilename))
    {
        if (!existingContents.Contains(csvHeader))
        {
            writetext.WriteLine(csvHeader);
        }
        writetext.WriteLine(csv);
    }
}

:\\程序文件(x86)\\ IIS防爆preSS C默认

在开发计算机中,CSV文件已保存。在preparation的,当它被部署到其最后的安息/工作的地方,我需要做什么才能让文件保存,例如,到服务器的Platypi文件夹 - 什么特别?我必须专门设置某些文件夹柿子以允许写入Platypi。

On the dev machine, the csv file is saved to "C:\Program Files (x86)\IIS Express" by default. In preparation for when it is deployed to its final resting/working place, what do I need to do to get the file to save, e.g., to the server's "Platypi" folder - anything special? Do I have to specifically set certain folder persimmons to allow writing to "Platypi."

是不是简单地改变这一行的问题:

Is it simply a matter of changing this line:

string csvFilename = string.Format("Platypus{0}.csv", dbContextAsInt);

...这样:

string csvFilename = string.Format(@"\Platypi\Platypus{0}.csv", dbContextAsInt);

推荐答案

在IIS的情况下,文件夹的应用程序有权利写在那里。我建议将文件写入App_Data文件夹。

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.

当要保存你必须给IIS应用程序池的服务帐户的IIS应用程序文件夹之外的文件(我认为它在默认情况下是NETWORKSERVICE),该文件夹的适当的权限。

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.

正如克莱B.香要求我的实现:

As requested by B. Clay Shannon my implementation:

string fullSavePath = HttpContext.Current.Server.MapPath(string.Format("~/App_Data/Platypus{0}.csv", dbContextAsInt));

这篇关于我可以写一个文件到一个文件夹服务器计算机上从上运行的Web应用程序的API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆