Solved

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

Posted on 2010-09-20
3
682 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…

726 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