Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 702
  • Last Modified:

How to get filename from uploaded file? asp.net VB

On other scripts I have used 'FileUploadID.fileName' to get the name of the file. However in a script I am trying to implement on my site I an error when trying to do the same thing:

'FileName' is not a member of 'System.Web.UI.HtmlControls.HtmlInputFile'.

So how do I get the file name of the file I am uploading in the file below?

fileFld.FileName gives me the whole string, but I want only the name, ie. image1.jpg

<%@ Page Trace="False" Language="vb" aspcompat="false" debug="true" validateRequest="false"%>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>

<SCRIPT language="VBScript" runat="server">
    Const Lx = 530  ' max width for thumbnails
    Const Ly = 356  ' max height for thumbnails
    Const upload_dir = "main_slides/"   ' directory to upload file
    Const upload_original = "original"    ' filename to save original as (suffix added by script)
    Const upload_thumb = "thumb"    ' filename to save thumbnail as (suffix added by script)
    Const upload_max_size = 25000  ' max size of the upload (KB) note: this doesn't override any server upload limits
    Dim OrigFileName As String ' Original file name
    Dim fileExt ' used to store the file extension (saves finding it mulitple times)
    Dim newWidth, newHeight As Integer ' new width/height for the thumbnail
    Dim l2  ' temp variable used when calculating new size
    Dim fileFld As HttpPostedFile   ' used to grab the file upload from the form
    Dim originalimg As System.Drawing.Image ' used to hold the original image
    Dim msg ' display results
    Dim upload_ok As Boolean    ' did the upload work ?
    Randomize() ' used to help the cache-busting on the preview images
    upload_ok = False
    If LCase(Request.ServerVariables("REQUEST_METHOD")) = "post" Then
        fileFld = Request.Files(0)  ' get the first file uploaded from the form (note:- you can use this to itterate through more than one image)
        If fileFld.ContentLength > upload_max_size * 1024 Then
            msg = "Sorry, the image must be less than " & upload_max_size & "Kb"
                originalimg = System.Drawing.Image.FromStream(fileFld.InputStream)
                ' work out the width/height for the thumbnail. Preserve aspect ratio and honour max width/height
                ' Note: if the original is smaller than the thumbnail size it will be scaled up
                If (originalimg.Width / Lx) > (originalimg.Width / Ly) Then
                    l2 = originalimg.Width
                    newWidth = Lx
                    newHeight = originalimg.Height * (Lx / l2)
                    If newHeight > Ly Then
                        newWidth = newWidth * (Ly / newHeight)
                        newHeight = Ly
                    End If
                    l2 = originalimg.Height
                    newHeight = Ly
                    newWidth = originalimg.Width * (Ly / l2)
                    If newWidth > Lx Then
                        newHeight = newHeight * (Lx / newWidth)
                        newWidth = Lx
                    End If
                End If

                Dim thumb As New Bitmap(newWidth, newHeight)

                'Create a graphics object
                Dim gr_dest As Graphics = Graphics.FromImage(thumb)

                ' just in case it's a transparent GIF force the bg to white
                Dim sb = New SolidBrush(System.Drawing.Color.White)
                gr_dest.FillRectangle(sb, 0, 0, thumb.Width, thumb.Height)

                'Re-draw the image to the specified height and width
                gr_dest.DrawImage(originalimg, 0, 0, thumb.Width, thumb.Height)

                    OrigFileName = upload_file.FileName
                    fileExt = System.IO.Path.GetExtension(fileFld.FileName).ToLower()
                    originalimg.Save(Server.MapPath(upload_dir & OrigFileName & upload_original & fileExt), originalimg.RawFormat)
                    thumb.Save(Server.MapPath(upload_dir & OrigFileName & upload_thumb & fileExt), originalimg.RawFormat)
                    msg = "Uploaded " & fileFld.FileName & " to " & Server.MapPath(upload_dir & upload_original & fileExt)
                    upload_ok = True
                    msg = "Sorry, there was a problem saving the image."
                End Try
                ' Housekeeping for the generated thumbnail
                If Not thumb Is Nothing Then
                    thumb = Nothing
                End If
                msg = "Sorry, that was not an image we could process."
            End Try
        End If

        ' House Keeping !
        If Not originalimg Is Nothing Then
            originalimg = Nothing
        End If

    End If
 <title>ASP.NET File Upload and Resize Sample</title>
 <META NAME="Description" content="ASP.NET File Upload and Resize Sample (Hybrid VB.NET)" />
 <META NAME="Keywords" content="ASP.NET, ASP, NET, VB, VBScript, Image, Upload, Resize, Thumbnail, Constrain, Filesize, File, Size, Free" />
 <META NAME="Copyright" content="Rufan-Redi Pty Ltd 2005" />
 <META NAME="Author" content="System developed by Jeremy at http://www.Rufan-Redi.com" />
 <p><b>Hybrid ASP.NET File Upload and Resize Sample (VB.NET)</b>
 <br/>Upload and resize a GIP/JPG/PNG images, ensuring filesizes are optimum.</p>
 <form id="Form1" enctype="multipart/form-data" method="post" runat="server">
 <tr><td>Select the file to upload:</td><td><input type="file" name="upload_file" id="upload_file" runat="server" /></td></tr>
 <tr><td colspan="2">Max upload size <%=upload_max_size%>Kb, gif/jpg/png only</td></tr>
 <tr><td colspan="2"><input type="submit" value="Upload" /></td></tr>
     If upload_ok Then
 <td valign="top"><img src="<%=upload_dir & upload_original & fileExt & "?" & rnd()%>"></td>
 <td valign="top"><img src="<%=upload_dir & upload_thumb & fileExt & "?" & rnd()%>"></td>
 <% Response.Write(msg)%>
 End If

Open in new window

1 Solution
Dustin HopkinsManaging MemberCommented:
This method uses system.io.path.

Dim strFilename as string = system.io.path.GetFileName(fileFld.FileName)

Hope this helps,
Instead of "<input type="file" name="upload_file" id="upload_file" runat="server" />" use asp.net file upload control

and getting the file name = "FileUpload1.FileName"
if you want the complete file name with the path then


for the Extension

elliottbenzleAuthor Commented:

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now