FileUpload丢失目录路径. [英] FileUpload loses directory path.

查看:102
本文介绍了FileUpload丢失目录路径.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有一个asp.net文件上传控件.我使用以下代码访问所选文件的路径和文件名:

I have an asp.net fileupload control on my page. I use the following code to access the path and filename of the file selected:

string = file.PostedFile.FileName; //file is the fileupload control



当我从服务器运行该网页时,一切正常-我得到了文件路径和文件名,但是当我远程运行该网页时,PostedFile.FileName属性丢失了目录路径,仅给出了所选文件的文件名,即问题,因为我需要文件的路径才能访问它.



When I run this webpage from the server, everything works fine - I get the filepath and filename, but when I run this webpage remotely, the PostedFile.FileName property loses the directory path and only gives the filename of the selected file, which is a problem because i need the path of the file in order to access it. Any help on how I can make the above work on remote logins just as on the server?

推荐答案

FileUpload和IE浏览器存在一个有趣的问题.

说,您在后面的代码中有以下代码段:

There is an interesting issue with FileUpload and IE browser.

Say, you have the following piece of code in the code behind:

string fileName = file.PostedFile.FileName;



如果从Firefox browser上传文件,您将在fileName变量(例如,AutoPlay.jpg)中仅得到文件名(无任何路径)(
)
但是,如果从IE上传文件,则会在用户计算机上从中上传文件的地方获得文件名以及路径(例如,"C:\Users\shubho\Desktop\AutoPlay.png").请注意,此路径不是Web服务器中的路径,这是客户端PC中的文件路径.

理想情况下,文件名不应包含目录路径,而且我也不知道IE为什么这样做(错误?).

可能是在从PC(而不是从服务器内部)进行测试时,您正在使用非IE浏览器,因此,您没有看到文件路径.

无论如何,您应该仅基于文件名而不是基于用户PC的文件路径(仅由IE发送)来编码程序.



If you upload the file from Firefox browser, you will get only the file name (Without any path) in the fileName variable (Say, AutoPlay.jpg)

But, if you upload the file from IE, you will get the File name along with the path in the user''s computer from where the file is uploaded (Say, "C:\Users\shubho\Desktop\AutoPlay.png"). Note that, this path is not the path in the web server, this is the file path in client''s PC.

Ideally, the file name should not contain the directory path and I don''t know why IE does that (A bug?).

May be, while you are testing from your PC (Not from within the server), you are using a non-IE browser and hence, you are not seeing the file path.

Any way, you should code your program based upon the file name only, not based upon the file path of user''s PC(That is sent by IE only).


访问文件,则需要将该文件保存在服务器中.将该文件上传到服务器,然后从服务器访问该文件.
Before accessing the file, you need to save that file in the server. Upload that file to the server and access that file from server.


您要引用的路径是文件所在的客户端路径.实际上,它是无用的信息.除非您在Intranet应用程序上工作,否则知道文件驻留在哪里有什么好处.

我正在维护一段代码,其中将客户端原始路径保存为文件信息的一部分.现在研究它,我想知道它为应用程序增加了什么价值.是的!该文件是从"c:\我的文档\ myfolder \ myfiles \ image.jpg"上传的!!

您只需要知道文件名,上传文件后就可以完全控制文件的存放位置.
The path you are refring to is the client side path where the file resides. In reality it is useless piece of information. What good is that to know where the file resided, unless you are working on intranet application.

I am maintaining piece of code in which the client side original path is saved as part of the file information. Now looking into it, I wonder what value it added to the application. Yeah! the file was uploaded from "c:\my documents\myfolder\myfiles\image.jpg" doh!.

All you need to know is the file name and once you upload the file you have a full control as to where you want it to live.


这篇关于FileUpload丢失目录路径.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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