Welcome, Guest: Join Nairaland / LOGIN! / Trending / Recent / New
Stats: 2,348,126 members, 5,208,481 topics. Date: Tuesday, 15 October 2019 at 02:38 AM

C# - Capturing File Name From A FileUpload Control In Asp.net - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / C# - Capturing File Name From A FileUpload Control In Asp.net (18259 Views) (2) (3) (4)

(1) (Reply) (Go Down)

C# - Capturing File Name From A FileUpload Control In Asp.net by csharpjava(m): 4:09pm On Jan 01, 2012
Happy new year to all NL programmers.

I'm looking for a way to capture the file name from a FileUpload control, I can get the full path if I use TextBox1.CliectID in the code below. But how do I capture just the file name a CheckBoxList or similarl?



protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { FileUpload1.Attributes["onchange"] = String.Format("document.getElementById('{0}').value=this.value;", TextBox1.CliectID);}
         
    }
Re: C# - Capturing File Name From A FileUpload Control In Asp.net by Gomez(m): 8:44pm On Jan 01, 2012
Hi
This is one of code I wrote some years back, It gets the file name without extension first, then it gets the extension of the uploaded file too.
You can then work with the two strings the way that suites the application requirement


if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0))
{


string fn = Path.GetFileNameWithoutExtension(FileUpload1.FileName);
string extension = Path.GetExtension(FileUpload1.FileName);

}
You should look at the "Path" class, thats where your solution is.

Cheers
Re: C# - Capturing File Name From A FileUpload Control In Asp.net by csharpjava(m): 8:56pm On Jan 01, 2012
@Gomez
Thanks for the code. I have tried it but it won't show the filename in a label when the user selects a file.
Re: C# - Capturing File Name From A FileUpload Control In Asp.net by Gomez(m): 10:15am On Jan 02, 2012
I assume this is your scenaro

1. when a user selects a file to upload, the file name of the selected file should show on a label somewhere in your page.

If this is the case, then a couple of things to notice, the server side asp.net file upload does not have a "selected item event" in order to get that information you might need to look at javascript and jquery.

There is a jquery mulitple file upload plugin that can give you the information of the selected file.

You look at it so it will help.
http://www.codeproject.com/KB/aspnet/multiple_file_upload.aspx

http://valums.com/ajax-upload/

cheers
Re: C# - Capturing File Name From A FileUpload Control In Asp.net by Beaf: 10:26am On Jan 02, 2012
.
Re: C# - Capturing File Name From A FileUpload Control In Asp.net by csharpjava(m): 11:39am On Jan 02, 2012
@Gomez

The code in my initial post uses javascript from the sever side, it displays the full file path in a textbox when the user selects a file, but when I try using a label nothing is displayed. Why does it work with a textbox, but now I want it displayed in CheckBoxList or similar when a user selects a file.

Thanks for the link I will try their sample code, which uses a CheckBoxList instead of a label this is even better as the user can delete a file from the list.
Re: C# - Capturing File Name From A FileUpload Control In Asp.net by Kobojunkie: 8:24am On Jan 05, 2012
A simple google search and woosh. , . . . google to the rescue

http://stackoverflow.com/questions/1804745/get-the-filename-through-javascript


Seriously, in london and allergic to google undecided undecided undecided undecided
Re: C# - Capturing File Name From A FileUpload Control In Asp.net by csharpjava(m): 9:34am On Jan 05, 2012
@Kobojunkie

Thanks for the link but thier solution does not solve it. Google is my best friend o, I always use it before coming to NL.

This link from a google search shows how to Post filename in a label from Fileupload control the innerHTML of the label is what is needed.

And with a few google search I was able to create this code to add only the the file name to a CheckBoxList. I have added the code below for anyone interested. The only problem is that you must manually create at least one Check Box Item first like this
<asp:listitem Value="1">Item 1</asp:listitem>
before the script will work. Does anyone have a better way so as to avoid manually creating at least one Check Box Item first? or is AJAX the only solution?
Re: C# - Capturing File Name From A FileUpload Control In Asp.net by taofeeq137(m): 11:16pm On Jul 04, 2015
csharpjava:
Happy new year to all NL programmers.

I'm looking for a way to capture the file name from a FileUpload control, I can get the full path if I use TextBox1.CliectID in the code below. But how do I capture just the file name a CheckBoxList or similarl?


FileUpload1.FileName().Replace(FileUpload1.Extension, ""wink;??

(1) (Reply)

Fingerprint Programming In ASP.NET / Inserting And Retrieving Images To/From Database In ASP.NET / Appointment Scheduler In Asp

How Much Will It Cost Me To Design a mobile APP In Nigeria / Where To Go If You Need Programmers Or Advertise Your Programming Skills / What Programming Language Is Used In Creating Viruses?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2019 Oluwaseun Osewa. All rights reserved. See How To Advertise. 42
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.