Search KB Filter article types
OpenFileDialog CodeSample - Getting filename of uploaded files
Categories: Common Dialogs
Tags: Developers, Windows & Dialogs, 1. Beginner, 2. Intermediate, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
Revision: 1
Posted: 05/Oct/2009
Updated: 31/Aug/2010
Status: Publish
Types: Code

This article will have a few sections added to it soon, based on the following article type skeleton: CodeSample
Overview

This OpenFileDialog codesample shows how to retrieve filenames of uploaded files, both the "real" filenames as they appear when you select them for upload, as well as the temporary filename assigned when the file has been uploaded to the webserver. The latter you will use when accessing the uploaded file from your server-side (code-behind) code after upload has completed.

VB.NET Code
Friend Sub test(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
     Dim ofd As OpenFileDialog = CType(sender, OpenFileDialog)
     txtTempNames.Text = ""
     For Each fn As String In ofd.FileNames
         txtTempNames.Text += fn + vbCrLf
     Next
     txtLocalNames.Text = ""
     For i As Integer = 0 To ofd.Files.Count - 1
         txtLocalNames.Text += CType(ofd.Files(i), HttpPostedFileHandle).PostedFileName + vbCrLf
     Next
End Sub

C# Code
private void mobjButton_Click(object sender, EventArgs e)
{
    // Invoke Dialog.
    mobjOpenFileDialog.ShowDialog(this, OpenFileDialogProcessed);
}
 
private void mobjOpenFileDialog_FileOk(object sender, CancelEventArgs e)
{
    OpenFileDialog objOFD = sender as OpenFileDialog;
    if (objOFD == null)
    {
        return;
    }
 
    HttpPostedFileHandle objFile = null;
    if (objOFD.Files[0] == null || !(objOFD.Files[0] is HttpPostedFileHandle))
    {
        return;
    }
    objFile = objOFD.Files[0] as HttpPostedFileHandle;
 
    // Display original file name, of the first uploaded file, on a label.
    mobjLabel.Text = System.IO.Path.GetFileName(objFile.PostedFileName);
}

RererencesCode samples

About the author

Related Articles

Common Dialogs  
Title Update Author
Tags: Architects, Developers, Windows & Dialogs, C#, VB.NET, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
04/Dec/2010    2010/12/04
Tags: Architects, Developers, Windows & Dialogs, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
07/Dec/2010    2010/12/07
Tags: Developers, Windows & Dialogs, 1. Beginner, 2. Intermediate, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
31/Aug/2010    2010/08/31
.NET HTML5 Web, Cloud and Mobile application delivery | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2012 Visual WebGui®       Visual WebGui weblog on ASP.NET Gizmox Blog Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS