Solved

How to Obtain the file extension using asp:FileUpload

Posted on 2009-05-20
2
655 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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

Suggested Solutions

Title # Comments Views Activity
Session on Html 8 42
Record locking on classic ASP 3 40
Calculating percentage 2 32
ExcelWebAddIn Connect to SQL database (Javascript or VB.net) 7 28
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

730 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