簡體   English   中英

在C#中獲取ASP.NET中的路徑

[英]Get Path in asp.net for 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));

這給出了項目的目錄

嘗試使用以下內容:

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

這為您提供了網站根目錄中的文件夾路徑。

編輯:-如果文件不在您的站點目錄樹中,則您應該知道用戶要將文件保存到哪個路徑。

您是否正在使用文件上載控件? 如果您是,則只需要他們選擇要上載的文檔,然后指定要保存該文檔的路徑。 例如

 // 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只是用戶登錄的userId。 這樣,其他用戶將不會覆蓋它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM