【发布时间】:2016-08-17 07:48:14
【问题描述】:
我有一个 web api,比如:
[Route("api/fileupload/{name}")]
public string GetFileWithExtension(string name)
{ private readonly string _recipientsExcelFilesPath = System.Web.Hosting.HostingEnvironment.MapPath(
WebConfigurationManager.AppSettings["RecipientsExcelFiles"]);
// _recipientsExcelFilesPath gives absolute URL to the directory
// name property is a name of file
// I would like here get absoulute url + path
**C:\Code\MyProject\Project.WebApi\Data\MyExcelFiles.xlsx**
}
因此,基本上,此方法采用文件名,并应返回保存在本地系统 + 扩展名上的该文件的完整路径。
在 web api 配置中我有类似的东西:
<appSettings>
<add key="RecipientsExcelFiles" value="~\\Data\\RecipientsExcelFiles\\" />
</appSettings>
【问题讨论】:
-
嗯,文件
C:\Code\MyProject\Project.WebApi\Data\MyExcelFiles没有扩展名。您希望从哪里获得.xlsx的值? -
只是因为您的查看器没有已知的扩展,操作系统和其他一切都知道它有一个扩展
-
如果您确定“绝对 url”的末尾总是有一个没有扩展名的文件名。您可以通过路径分隔符上一级并列出该目录中的文件然后匹配文件名然后提取有效的扩展名?
标签: c# .net system.io.file