user1776590
user1776590

Reputation: 135

Get Path in asp.net for c#

I am trying to get a path for a file which a users uploads but the path that I am getting is wrong because the path should be for an example

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

but on my program I get

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

the code that I am using is

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

I also tried using

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

this give the directory of the project

Upvotes: 0

Views: 1347

Answers (2)

MVCKarl
MVCKarl

Reputation: 1295

Are you using a File Upload control? If you are, you just need them to select the document they want to upload and then specify the path you want to save it at. For example

 // 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);

Note: UserId is just the users login userId. This way other users wont override it.

Upvotes: 1

TheVillageIdiot
TheVillageIdiot

Reputation: 40537

try using following:

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

This gives you the folder path from the root directory of your website.

EDIT:- You should be knowing to which path users are saving the file if it is not in your site directory tree.

Upvotes: 2

Related Questions