Link to home
Start Free TrialLog in
Avatar of elliottbenzle
elliottbenzle

asked on

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

Thanks
<%@ 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 ?
</SCRIPT>
<%
    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"
        Else
            Try
                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
                Else
                    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)

                Try
                    
                    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
                Catch
                    msg = "Sorry, there was a problem saving the image."
                End Try
                ' Housekeeping for the generated thumbnail
                If Not thumb Is Nothing Then
                    thumb.Dispose()
                    thumb = Nothing
                End If
            Catch
                msg = "Sorry, that was not an image we could process."
            End Try
        End If

        ' House Keeping !
        If Not originalimg Is Nothing Then
            originalimg.Dispose()
            originalimg = Nothing
        End If

    End If
 %>
 <html>
 <head>
 <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" />
 </head>
 <body>
 
 <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">
 <table>
 <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>
 </table>
 </form>
 
 <%
     If upload_ok Then
%>
 <table>
 <tr>
 <td valign="top"><img src="<%=upload_dir & upload_original & fileExt & "?" & rnd()%>"></td>
 <td valign="top"><img src="<%=upload_dir & upload_thumb & fileExt & "?" & rnd()%>"></td>
 </tr>
 </table>
 <% Response.Write(msg)%>
 <%
 Else
     Response.Write(msg)
 End If
 %>
 </body>
 </html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dustin Hopkins
Dustin Hopkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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


"FileUpload1.PostedFile.FileName";

for the Extension

System.Io.Getextension(FileUpload1.PostedFile.FileName);
Avatar of elliottbenzle
elliottbenzle

ASKER

Thanks