Link to home
Start Free TrialLog in
Avatar of baasassasin
baasassasin

asked on

Changing Filename via code using FileUploader Control

This is a two fold question:
  1) Could someone show me in code how to make a fileuploader control that is like the current one on the visual toolbar?  The one there is fine but lets say I want to change the name of the browse button or I want to change the location of the textbox for the path. I don't see a way of doing that in the properties so making it myself would be a better sollution I would think. I understand how to make the textbox and the onclick event but the reason I picked this control was for the fact it allowed for browsing.

2) How do you change the name of a file that has been uploaded. Lets say for instance you have  dsc1001.jpg but you want the picture be matched to that of an employeeID say 7  how would you change it to 7.jpg?   I know you could make filename.text or whatever = to 7.jpg but that would mess up the actual source path before upload would it not?  What I'm shooting for is the ability to upload an image and then when it post to the server have the server instantly change its name to <EmployeeID>.jpg

I know how to to use the UI control but I want to see this done manual in code so it can be modified. Thanks in advance for your help.
ASKER CERTIFIED SOLUTION
Avatar of rodmjay
rodmjay
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
Avatar of baasassasin
baasassasin

ASKER

Thanks that is the answer I was looking for. I will try to implement this into my code and will ask additional question if any arrise. Thanks again.
One more question for you. First off I modified your code to meet my needs and converted it into a modular subroutine. In any event, I can't seem to understand what siHiExtention needs to be declared at and the compiler is shakin its finger at me saying sHiExtention is not declared. Could you please help thanks.
Here is my code:

#Region "UPload a Jpeg Function"
    Public Sub UploadAnImage(ByVal FilUpload As System.Web.UI.WebControls.FileUpload, ByVal FileUploadReport As Label, ByVal ImgPrevew As Image, ByVal EmployeeID As Integer)

        Dim sSavePath As String
        sSavePath = ResolveUrl("C:\Inetpub\pedalvalve\Mugshots\")
        Try
            'If file field isn't empty
            If Not FilUpload.PostedFile Is Nothing Then

                'Check file size
                Dim myFile As HttpPostedFile = FilUpload.PostedFile
                Dim nFileLen As Integer = myFile.ContentLength
                If nFileLen = 0 Then
                    FileUploadReport.Text = "No file was uploaded."
                    Return
                End If

                'Check file extension
                If Not System.IO.Path.GetExtension(myFile.FileName).ToLower = ".jpg" Then
                    FileUploadReport.Text = "The file must have an extension of JPG"
                    Return
                End If

                'Read file into data stream
                Dim myData(nFileLen) As Byte
                myFile.InputStream.Read(myData, 0, nFileLen)

                'Make sure a duplicate file doesn't exist. If it does, keep on appending an
                'incremental numeric unit until its unique
                Dim sFilename As String = EmployeeID + ".jpg"

                While System.IO.File.Exists(Server.MapPath(sSavePath + sFilename))
                    System.IO.File.Delete(Server.MapPath(sSavePath + sFilename)) '' delete the old image and store the new one!
                    sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + ".jpg"
                End While

                'Save the steam to disk
                Dim newFile As System.IO.FileStream = _
                    New System.IO.FileStream(Server.MapPath(sSavePath + String.Format(sFilename, sHiExtension)), IO.FileMode.Create)
                newFile.Write(myData, 0, myData.Length)
                newFile.Close()



                'Displaying success information
                FileUploadReport.Text = "File uploaded successfully!"





            End If
        Catch ex As Exception

            'The file wasn't a valid jpg file
            FileUploadReport.Text = "The file wasn't a valid jpg file."

            Throw ex
        End Try


    End Sub
#End Region
oh, sorry, what i was doing in this case was saving three versions of the same file, for example the hi resolution file, the lo resolution file, etc.

You can simple ignore these types of things, delete the entire string.format clause and replace with any string you want your files to be named.
Still a bit confused rodmjay
I'm not getting any errors nor Is the file being placed on the server. I set a breakpoint in the debugger but for some reason  beyond me its not breaknig so I can see what the path path variable is.
have a look at http://pastebin.com/685519 thats the codebehind for my addemployee noticed the highlight on line 310

here is my class.....
please take note of the highlighted section which is the subroutine for the update method http://pastebin.com/685529

Thanks in advance. Almost got it but still playing battleship and missing the boat ;)
Here is the link from which i got the code, i modified it, but here is the real thing

http://www.codeproject.com/aspnet/netimageupload.asp