Solved

How to Obtain the file extension using asp:FileUpload

Posted on 2009-05-20
2
650 Views
Last Modified: 2012-05-07
Hi All,

I am using the asp.net control asp:FileUpload and c# in my code behind based on MS' example here http://msdn.microsoft.com/en-us/library/aa479405.aspx .

Though, I am going to rename the file using the user's session id for uniqueness but I need to then make sure that the file's correct extension is added back onto my .SaveAs argument.

Please could anyone suggest how this could be achieved?

Many thanks,

Rit
<%@ Page Language="C#" %>
 

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)

    {

        if (FileUpload1.HasFile)

            try

            {

                     string strSession;

                strSession = Session.SessionID;
 

                FileUpload1.SaveAs("C:\\Uploads\\" + 

                     strSession);

                Label1.Text = "File name: " +

                     FileUpload1.PostedFile.FileName + "<br>" +

                     FileUpload1.PostedFile.ContentLength + " kb<br>" +

                     "Content type: " +

                     FileUpload1.PostedFile.ContentType;

            }

            catch (Exception ex)

            {

                Label1.Text = "ERROR: " + ex.Message.ToString();

            }

        else

        {

            Label1.Text = "You have not specified a file.";

        }

    }

</script>

Open in new window

0
Comment
Question by:rito1
2 Comments
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 500 total points
ID: 24430344
Hi rito1,
Try this:
string strext=System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower;
FileUpload1.SaveAs("C:\\Uploads\\" +  strSession + strext);

More details for asp upload control:
http://aspalliance.com/1614_Adding_Filter_Action_to_FileUpload_Control_of_ASPNET_20.all
               
0
 
LVL 1

Author Closing Comment

by:rito1
ID: 31583442
Excellent, thanks. Rit
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now