简体   繁体   English

文件路径不正确

[英]File path not taking correctly

Inside my asp.net website I am trying to access a file 在我的asp.net网站中,我正在尝试访问文件

 FileStream stream = File.Open("/App_Data/" + FileName, FileMode.Open, FileAccess.Read);

I want this code to select an xlsxfile inside my websites app data folder. 我希望此代码在我的网站应用程序数据文件夹中选择一个xlsxfile。 This code always take the whole path from directory like 此代码始终从目录获取整个路径,例如

'c:\App_Data\w1.xlsx

I don't want this code to go for root URL.I just want to take file inside my website.How can i tweek the code to achieve this 我不希望此代码用于根URL,我只想在我的网站内保存文件,我如何才能将这些代码实现此目标

使用Server.MapPath("~/App_Data/" + FileName)代替"/App_Data/" + FileName

Try this: 尝试这个:

File.Open(Server.MapPath("~") + FileName, FileMode.Open, FileAccess.Read);

Also check out this answer, it has pretty good explanation of MapPath 还要看看这个答案,它对MapPath有很好的解释

Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\\"), Server.MapPath("/"). Server.MapPath(“。”),Server.MapPath(“〜”),Server.MapPath(@“ \\”),Server.MapPath(“ /”)。 What is the difference? 有什么区别?

尝试使用

Server.MapPath('~/App_Data/' + FileName)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM