Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

upload files

Status
Not open for further replies.

lunchbox88

Programmer
Feb 17, 2004
208
0
0
US
If I want a user to upload a file, what's the best way to go about this? If I use
Code:
<input type="file">
how do I get reference that value in my code behind?
 
<input type=file runat=server id=uplFile>

Server side

Protected WithEvents uplFile As System.Web.UI.HtmlControls.HtmlInputFile

Then in code

Dim objFile As HttpPostedFile = uplFile.PostedFile
objFile.saveas("c:\myfile.extn")

To check if a file was uploaded check

if obJFile.ContentLength > 0 then
'File was uploaded
end if
 
Here is a little piece of code which upload a file on web server in a particular folder.

'In the ASPX Form
<form id="frmName" method="post" encType="multipart/form-data" runat="server">

<INPUT type="file" name="txtImgFile">

'In Code Behind

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim vSaveFilePath As String = Nothing
Dim vImageFile As String = Nothing
Dim vFileName As HttpPostedFile
Dim vFileType As String
Dim vSplit() As String
Dim i As Integer = 0
Dim vFileSize As Integer

'- Upload Image Image
vSaveFilePath = Request.PhysicalApplicationPath
For i = 0 To Request.Files.Count - 1
vFileName = Request.Files(i)
If vFileName.FileName <> "" Then
vImageFile = vFileName.FileName
vFileSize = vFileName.ContentLength
vFileType = vFileName.ContentType

If System.IO.Path.GetExtension(vImageFile) <> ".jpg" Then
'Show Here Image Type Error Window
Exit Sub
End If

If CStr(vFileSize) > 81920 Then
'80KB = 81920 Bytes
'Show Here Size Error Window
Exit Sub
End If

vSplit = Split(vImageFile, "\")
vImageFile = vSplit(UBound(vSplit))
vSplit = Split(vImageFile, ".")
vImageFile = Me.txtCatName.Text.Trim & "-" & i + 1 & "." & vSplit(1)
vFileName.SaveAs(vSaveFilePath & "Images\Category\" & vImageFile)
End If
Next


Priyesh
priyeshdeshpande@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top