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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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\ped alvalve\Mu gshots\")
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.GetExtensio n(myFile.F ileName).T oLower = ".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(my Data, 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(Serv er.MapPath (sSavePath + sFilename))
System.IO.File.Delete(Serv er.MapPath (sSavePath + sFilename)) '' delete the old image and store the new one!
sFilename = System.IO.Path.GetFileName WithoutExt ension(myF ile.FileNa me) + ".jpg"
End While
'Save the steam to disk
Dim newFile As System.IO.FileStream = _
New System.IO.FileStream(Serve r.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
Here is my code:
#Region "UPload a Jpeg Function"
Public Sub UploadAnImage(ByVal FilUpload As System.Web.UI.WebControls.
Dim sSavePath As String
sSavePath = ResolveUrl("C:\Inetpub\ped
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.GetExtensio
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(my
'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(Serv
System.IO.File.Delete(Serv
sFilename = System.IO.Path.GetFileName
End While
'Save the steam to disk
Dim newFile As System.IO.FileStream = _
New System.IO.FileStream(Serve
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.
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.
ASKER
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 ;)
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
http://www.codeproject.com/aspnet/netimageupload.asp
ASKER