Path.GetFullPath给出不好的路径 [英] Path.GetFullPath given bad path

查看:606
本文介绍了Path.GetFullPath给出不好的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我选择上传一个文件名为about.png这是在位置C:\\影像\\ about.png
但是,当我得到一个文件未找到错误后,通过code步骤我看到它总是路径设置为这个
C:\\ Program Files文件(x86)的\\ Common Files文件\\ Microsoft共享\\ DevServer \\ 10.0 \\ about.png

I am choosing to upload a file named about.png which is in location "C:\images\about.png" But when I step through the code after getting a file not found error I see it is always setting the path to this "C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\about.png"

我没有上传这我的Web服务器,得到了相同的结果,所以它不是一个局部问题。
奇怪的是Visual Studio是安装在E盘不要放在C盘而增加了更多的混乱上。感谢您事先的任何帮助。

I did upload this to my web server and got the same result so it is not a local issue. The strange thing is Visual Studio is installed on the E drive not the C drive which adds even more confusion. Thanks in advance for any help.

下面是code。

request.FilePath = Path.GetFullPath(filefield.FileName);

补充说明:该文件可能来自任何地方的用户会选择一个资料图片。我忘了说,这是一个上载过程

推荐答案

上传的文件内容是HTTP请求的一部分 - 的ASP.NET文件上传控件不将文件保存在磁盘上。您可以通过使用另存为做手工方法。

The uploaded file contents are part of HTTP request - the ASP.NET File Upload control does not save the file on the disk. You have to do that manually by using SaveAs method.

的FileName属性只提供的文件名,因为路径信息将是从客户机和它没有在服务器计算机上的任何相关性。所以,如果你尝试
Path.GetFullPath 来获取文件名的完整路径 - 它是唯一的相对路径,将给道路w.r.t.当前目录 - 在你的情况下,原来是道路发展的Web服务器(随Visual Studio中)

The FileName property provides only the file name because path information will be from client machine and it does not have any relevance on the server machine. So if you try Path.GetFullPath to get the full path on the file name - it being only relative path, will give path w.r.t. current directory - in your case, it turns out to be the path development web server (that comes with Visual Studio).

通常情况下,你应该使用另存为方法上传文件存储在Web服务器上指定的位置 - 同时节约应该是一个生成的一个使用的文件名(例如GUID或某些其他标识),并在数据库中与所产生的文件名一起存储实际文件名。这是因为上传的文件名可能无法在服务器计算机上有效的(考虑有人上传文件从Mac或Linux机器)。还有一种方法是到磁盘上的文件保存在临时位置,然后将内容推入数据库本身。

Typically, you should use SaveAs method to store the upload file at the designated location on the web server - the file name used while saving should be a generated one (say guid or some other id) and store the actual file name in the database along with the generated file name. This is because the upload file name may not be valid on the server machine (consider someone uploads file from MAC or Linux machine). Yet another way is to save the file on disk at temp location and then push the contents into the database itself.

这篇关于Path.GetFullPath给出不好的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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