简体   繁体   English

Asp.Net C#获取文件的完整路径

[英]Asp.Net C# Getting Full Path of a File

I am building a web application using WCF. 我正在使用WCF构建Web应用程序。 I need to full path of a file to open and upload it to web service. 我需要完整的文件路径才能打开并将其上传到Web服务。 What I am doing is first opening a file by its full path, then take stream of it and finally upload it to service. 我正在做的是首先按完整路径打开文件,然后对其进行流处理,最后将其上传到服务中。 My code is below 我的代码如下

string Path = Server.MapPath( FileUpload1.PostedFile.FileName);

System.IO.Stream fileStream = File.OpenRead(@Path);

I can't get full path of the file for security reasons. 出于安全原因,我无法获取文件的完整路径。

How can I read the file that users select? 如何读取用户选择的文件?

Server.MapPath(FileUpload1.FileName) is the path to a file on the server. Server.MapPath(FileUpload1.FileName)是服务器上文件的路径。 You cannot get the full path of the client machine of the file Using the FileUpload . 您无法使用FileUpload获取文件客户端计算机的完整路径。

There is no need for the full client path to access it. 不需要完整的客户端路径来访问它。 You can use the FileBytes property in order to read the contents of uploaded file. 您可以使用FileBytes属性来读取上载文件的内容。

As others have already suggested there's no reason you need the file path to the client in order to save a file on your server. 正如其他人已经建议的那样,您没有理由需要客户端的文件路径来将文件保存在服务器上。 In the case you need some clearer explanation for this, then please refer to these answers: 如果您需要对此进行更清晰的说明,请参考以下答案:

https://stackoverflow.com/a/3077008/2196675 https://stackoverflow.com/a/3077008/2196675

https://stackoverflow.com/a/1130718/2196675 https://stackoverflow.com/a/1130718/2196675

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

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