ASP .NET 无效路径
我有一个网页,提示用户使用文件上传控件输入 Excel 文件。然后它所做的就是使用 OleDbConnection 将文件读入数据表,然后使用该数据运行其他代码。当我在 Visual Studio 中测试时,它工作正常。例如,我可以查找文件“g:\myfiles\upldtest.xls”,它找到该文件,读取它并且代码可以工作。当我尝试在 Web 服务器上运行它时,当它尝试创建 OleDBConnection 时出现错误,提示“它正在尝试创建 OleDbConnection 并且路径 'g:\myfiles\upldtest.xls' 无效”。
我尝试使用 ManagementObjectSearcher 将连接字符串路径转换为 UNC(\\MyDataServer\myfiles 而不是 g:\myfiles)。当我测试它时,它显示了正确的路径,但是当我将页面上传到网络服务器时,我仍然得到路径“g:\myfiles\upldtest.xls”无效。
我用来确定所需文件名的代码是这个
string tname = FileUpload1.PostedFile.FileName; //文件名和路径
字符串 gname = tname.Substring(tname.LastIndexOf("\\") + 1); //路径名
有什么想法我缺少什么吗?我的合同要求我使用 VS2005 和 .NET Framework 2.0,所以我不能使用任何更新的东西。预先感谢您的帮助。
I have a web page which prompts the user for an excel file using the fileupload control. What it then does is read the file into a datatable using an OleDbConnection and then runs other code with that data. When I test in Visual Studio, it works fine. For example, I can look up a file 'g:\myfiles\upldtest.xls', it finds the file, reads it and the code works. When I try to run it on our web server, I get an error when it tries to create an OleDBConnection saying It is trying to create an OleDbConnection and the path 'g:\myfiles\upldtest.xls' is invalid.
I tried to use ManagementObjectSearcher to convert the connection string path to UNC (\\MyDataServer\myfiles instead of g:\myfiles). When I test it, it shows the correct path but when I upload the page to the web server, I still get the path 'g:\myfiles\upldtest.xls' is invalid.
The code I use to determine the required file name is this
string tname = FileUpload1.PostedFile.FileName; //the file name and path
string gname = tname.Substring(tname.LastIndexOf("\\") + 1); //The path name
Any ideas what I am missing? My contract requires me to use VS2005 and .NET framework 2.0 so, I can't use anything newer. Thanks in advance for the assistance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还应该注意跨浏览器问题。 IE 在文件上传时将整个路径发送到服务器,而 Firefox/Chrome 则不会。
You should also be aware of cross-browser issues. IE sends the whole path to the server on file upload, while Firefox/Chrome do not.
HttpPostedFile.FileName 返回客户端计算机上文件的完全限定名称。
您需要调用 另存为() 将文件实际保存在服务器上:
IIS
可能已经将文件写入临时位置以节省内存,但由于您无法(也不应该)访问那个位置,没有什么区别。HttpPostedFile.FileName returns the fully qualified name of the file on the client machine.
You need to call SaveAs() to actually save the file on the server:
IIS
might have already written the file in a temporary location to save memory, but since you can't (and shouldn't) access that location, it makes no difference.