Solved

How to Obtain the file extension using asp:FileUpload

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…

724 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