Solved

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

Posted on 2010-09-20
3
676 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:elliottbenzle
3 Comments
 
LVL 14

Accepted Solution

by:
Dustin Hopkins earned 500 total points
ID: 33721902
This method uses system.io.path.

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

Hope this helps,
Dustin
0
 
LVL 3

Expert Comment

by:revaluser
ID: 33723342
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);
0
 
LVL 4

Author Closing Comment

by:elliottbenzle
ID: 33847411
Thanks
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now