How to create a File Picker in ASP.NET

Platform: ASP.NET| Category: DataList

Use namespace System.IO


<asp:label id='lblHeader' runat='server'></asp:label><br>
<b>Get Information on Directory:</b><br>
<p><asp:textbox id='txtPath' runat='server'></asp:textbox>
<p></p>
<asp:button id='btnSubmit' runat='server' text='Go!' type='Submit'></asp:button>
<p></p>
<div style='width:100%; height:200; overflow:auto;'>
<asp:datalist id='DataList1' runat='server' DataKeyField='FullName' OnSelectedIndexChanged='SelectedIndexChanged'>
	<ItemTemplate>
	<li>
		<%# DataBinder.Eval(Container.DataItem, 'Name') %>
		<br>
		<font size='-1'>[
		<asp:linkbutton Text='View Contents' CommandName='Select' runat='server' ID='Linkbutton1' />] 
			| [
		<%# DataBinder.Eval(Container.DataItem, 'Length') %>
		bytes] </font>
		<p>
	</ItemTemplate>
</asp:datalist>
</div> 
<p>
<hr>
</p><asp:label id='lblFileContents' runat='server'></asp:label>

VB.NET


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ’Put user code to initialize the page here
        If Request('txtPath') <> '' Then
            	Dim strDir As String = Request('txtPath')
            	lblHeader.Text = 'File Listing for ' & strDir & ''
            	Dim dirInfo As New DirectoryInfo(strDir)
            	’ Get the files for the directory strDir
            	Dim fInfos As FileInfo() = dirInfo.GetFiles('*.*')
            	DataList1.DataSource = fInfos
            	DataList1.DataBind()
        End If
End Sub

Protected Sub SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataList1.SelectedIndexChanged
        Dim strFilePath As String = DataList1.DataKeys(DataList1.SelectedItem.ItemIndex).ToString()
        Dim fInfo As FileInfo = New FileInfo(strFilePath)
        Dim objStream As StreamReader = fInfo.OpenText()
        Dim strContents As String = objStream.ReadToEnd()
        objStream.Close()

        lblFileContents.Text = 'Contents of ' & fInfo.Name & ':' & _
                               '' & vbCrLf & strContents & vbCrLf & ''
End Sub

C#


private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	if (Request['txtPath']!= null) 
	{
		string strDir   = Request['txtPath'];
		lblHeader.Text = 'File Listing for ' + strDir + '';
		DirectoryInfo dirInfo =new DirectoryInfo(strDir);

		// Get the files for the directory strDir
		FileInfo[] fInfos = dirInfo.GetFiles('*.*');
		DataList1.DataSource = fInfos;
		DataList1.DataBind();
	}
}

protected void SelectedIndexChanged(Object sender   , System.EventArgs e   )  
{
	string strFilePath    = DataList1.DataKeys[(int)DataList1.SelectedItem.ItemIndex].ToString();
	FileInfo fInfo    = new FileInfo(strFilePath);
	StreamReader objStream    = fInfo.OpenText();
	string    strContents   = objStream.ReadToEnd();
	objStream.Close();
	lblFileContents.Text = 'Contents of ' + fInfo.Name + ':' +  '' + '
' + strContents + '
' + '';
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.