2012-11-06 42 views
0

我试图得到一个文件的路径其中用户上传但我得到的路径是错误的,因为路径应该是一个例子获取路径asp.net中为C#

"C:\\Users\\Tranga.Patel\\Downloads\template.xlsx" 

但在我的计划,我得到

"c:\\windows\\system32\\inetsrv\\Template Final.xlsx" 

,我使用的代码

fileName = Path.GetFullPath(fileUpload.PostedFile.FileName); 

我使用

也尝试
fileName = Server.MapPath(Path.GetFullPath(fileUpload.PostedFile.FileName)); 

这给使用下列项目

+0

fileUpload.PostedFile.FileName将在Internet Explorer中提供完整路径,但其他浏览器只会提供该文件的名称。或者你不是在网站上工作? – PcPulsar

+0

我在网上工作,但我使用的浏览器是铬..但人们应该使用任何浏览器和代码应该仍然工作。 – user1776590

+0

你能告诉我为什么你想要客户的路径?当文件发布到服务器时,您无法正确获取客户端的路径。而且,你根本不需要这条路。 – PcPulsar

回答

1

你使用文件上传控件吗?如果你是,你只需要他们选择他们想上传的文档,然后指定你想保存的路径。例如

// Get File Name 
documentName = Path.GetFileName(fuContentDocuments.FileName); 

// Specify your server path 
string serverPath = Server.MapPath("../../" + WebConfigurationManager.AppSettings["FilePath"].ToString()); 

// The final path 
string fileLocation = (serverPath + "\\" + userId + "\\" + documentName); 

// if folder doesn't exist then create it 
if (!Directory.Exists(serverPath + "\\" + userId + "\\")) 
{ 
    // create the folder for the file 
    Directory.CreateDirectory(serverPath + "\\" + userId + "\\"); 
} 

// Upload the file 
fuContentDocuments.SaveAs(fileLocation); 

注意:UserId只是用户登录的用户标识。这样其他用户不会重写它。

2

尝试的目录:

var path=Server.MapPath('Uploads folder path from root directory'); 

这使您从您的网站的根目录的文件夹路径。

编辑: - 你应该知道用户保存文件的路径,如果它不在你的站点目录树中。

+0

这也给实际项目的根目录,而不是用户从 – user1776590

+0

上传该数据的路径,该文件可以保存在用户direcory的任何地方,我将有多个用户一次使用该程序,并且不能相同的文件上传两次 – user1776590