Solved

How to Obtain the file extension using asp:FileUpload

Posted on 2009-05-20
2
651 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

896 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

14 Experts available now in Live!

Get 1:1 Help Now