Link to home
Start Free TrialLog in
Avatar of fantasylan
fantasylan

asked on

How to get the entire file path from a FileUpload control in C sharp ASP.net

Hi,

I am using the FileUpload Control to attach emails in my application.
The problem is that I cant find out the entire file path and I need it so that the file can be attached to my email.

Thanks
Avatar of silemone
silemone
Flag of United States of America image

string filePath = (System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName));
Avatar of fantasylan
fantasylan

ASKER

Hi silemone,

I did what you suggested but instead of getting the actual location of the file, I was getting a path from inside visual studio as "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\gimp-2.6.exe". When I tried to attach this I got a runtime error "Can't find this file. Make sure the path and file name are correct."
Also, I checked to see if the file actually existed in file path that was being returned from the expression you suggested and there was no such file in the folder.
?  wow...that's odd.
are you running a local copy of your program?  maybe visual studios is working off of temporary files...bu tstill doesn't explain why it would give that address if its not correct...
I am running the application off Visual Studio.

Here is the code snippet I have written :

 string fpath = System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName);
        newAttachmentCell.Text = FileUpload1.FileName.ToString();
        newAttachmentRow.Cells.Add(newAttachmentCell);
        AttachmentTable.Rows.Add(newAttachmentRow);
        eMail.Attachments.Add(fpath, OlAttachmentType.olByValue, 1, FileUpload1.FileName);

Thanks
will the file always be located at a certain directory because you could always hardcode that part...

filePath = @"c://....";

yes ... i could always do that ;) ... but I wanted to implement it using the FileUpload box.
string strFilePath = Server.MapPath("~/UploadFile") + "\\" +FileUpload1.FileName;
Hi RajasekaranMoorthy,

The code that you posted points me to the directory where my Project Folder is located and not the actual file and I try to pass the the path of the file as an attachment to the email, I get a file not found runtime exception.
ASKER CERTIFIED SOLUTION
Avatar of maheshsnkr
maheshsnkr
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial