Search KB Filter article types
OpenFileDialog CodeSample - Upload single File and find out real Filename
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: Article

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

This OpenFileDialog codesample demonstrates how to retrieve the "real" file-name of an uploaded file.
"Real" in this context is the name of the file as it appears when you choose it for upload, not the temporary name assigned when uploaded.

VB.NET Code
Private Sub mobjButton_Click(sender As Object, e As EventArgs)
    'Invoke Dialog.
    mobjOpenFileDialog.ShowDialog(Me, OpenFileDialogProcessed)
End Sub
 
Private Sub mobjOpenFileDialog_FileOk(sender As Object, e As CancelEventArgs)
    Dim objOFD As OpenFileDialog = TryCast(sender, OpenFileDialog)
    If objOFD Is Nothing Then
        Return
    End If
 
    Dim objFile As HttpPostedFileHandle = Nothing
    If objOFD.Files(0) Is Nothing OrElse Not (TypeOf objOFD.Files(0) Is HttpPostedFileHandle) Then
        Return
    End If
    objFile = TryCast(objOFD.Files(0), HttpPostedFileHandle)
 
    'Display original file name, of the first uploaded file, on a label.
    mobjLabel.Text = System.IO.Path.GetFileName(objFile.PostedFileName)
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);
}

ReferencesForum threads

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 Web, Cloud and Mobile application delivery platform | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2011 Visual WebGui®       Visual WebGui weblog on ASP.NET Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS