?
Solved

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

Posted on 2009-12-23
11
Medium Priority
?
679 Views
Last Modified: 2012-05-08
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
0
Comment
Question by:fantasylan
11 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 26114596
string filePath = (System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName));
0
 

Author Comment

by:fantasylan
ID: 26114775
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.
0
 
LVL 21

Expert Comment

by:silemone
ID: 26114820
?  wow...that's odd.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 21

Expert Comment

by:silemone
ID: 26114826
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...
0
 

Author Comment

by:fantasylan
ID: 26114896
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
0
 
LVL 21

Expert Comment

by:silemone
ID: 26114923
will the file always be located at a certain directory because you could always hardcode that part...

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

0
 

Author Comment

by:fantasylan
ID: 26115756
yes ... i could always do that ;) ... but I wanted to implement it using the FileUpload box.
0
 
LVL 1

Expert Comment

by:RajasekaranMoorthy
ID: 26117367
string strFilePath = Server.MapPath("~/UploadFile") + "\\" +FileUpload1.FileName;
0
 

Author Comment

by:fantasylan
ID: 26119019
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.
0
 
LVL 4

Accepted Solution

by:
maheshsnkr earned 2000 total points
ID: 26300678
hi  fantasylan,

You can't directly assign the file path from the FileUpload Control.
Please go through following stepts which will be use full..

1. Upload the file by browing the File Upload Control. (I have put an Upload button(Button1) to save the file in the server.)
2. Save the file in the server (Where we host the application) with a unique id.
3. Read the file from the saved location (within the server) while sending the mail.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DirInfo As DirectoryInfo
        Dim SavedFileName As String = FileUpload1.FileName
        Try
            DirInfo = New DirectoryInfo(Server.MapPath("~") & "/FileUploads/UserFiles/Images/")
            If DirInfo.Exists = False Then
                Directory.CreateDirectory(DirInfo.FullName)
            End If

            Dim DestPath As String = (Server.MapPath("~") & "\FileUploads\UserFiles\Images\") + SavedFileName
            FileUpload1.SaveAs(DestPath)

            ViewState("SavedFilePath") = DestPath

        Catch generatedExceptionName As Exception

        End Try
    End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub SendMail()
        'The following code is used to retreive the file that needs to be attached
        'In the place of attaching the file within the mail, Read the filename from the Viewstate
        'Am just showing you , How to retrieve the data from the Viewstate
        CType(ViewState("SavedFilePath"), String))
    End Sub

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question