Upload this file upload should be able to meet the general needs, where the added to the database records, you can be omitted as needed.
Code: upload.aspx
<%@ Page language= "VB"%>
<% @ Import namespace= "System.IO"%>
<% @ Import namespace= "System.Data"%>
<%@ import namespace= "System.Data.SqlClient"%>
<script LANGUAGE = "VB" RUNAT = "SERVER" >
Dim Lstrfilefolder As String
Dim lstrfilefolder_1 As String
Dim Lstrfilenamepath As String
Dim lstrfilenamepath_1 As String
Sub uploadfile_clicked (Sender as Object, e as EventArgs)
Dim Lstrfilename As String
' Dim Lstrfilefolder As String
Dim cmdfilename As String ' form Cmdfilename value
Cmdfilename = Request ("Cmdfilename")
'''
'''
' If the upload directory is empty, use ' upload ' as the default upload directory
' Get the directory name uploaded to the server
If Dir.value <> "" Then
lstrfilefolder_1 = Dir.value & "/"
Lstrfilefolder = Trim (Server.MapPath ("")) & "\" & Dir.value & "\"
Else
lstrfilefolder_1 = "upload/"
Lstrfilefolder = Trim (Server.MapPath ("")) & "\upload\"
End If
' Get file name
Lstrfilename = LoFile.PostedFile.FileName
' Note: LoFile.PostedFile.FileName returns the
' File name selected through the file dialog box, which contains the directory information for the file
' only upload. xls files
If InStr (Path.getfilename (Lstrfilename), ". xls") =0 and InStr (Path.getfilename (Lstrfilename), ". Xls ") =0 and InStr (Path.getfilename (Lstrfilename),". XLS ") =0 Then
messagenotxls.innerhtml = "only upload. xls files"
Messagenotxls.style ("color") = "Red"
Exit Sub
End If
If Cmdfilename = "random" Then
Dim lstrFileName111 As String = Path.getfilename (lstrfilename) ' acquired original
Lstrfilename=now ()
Lstrfilename=replace (Lstrfilename, "-", "")
Lstrfilename=replace (Lstrfilename, ":", "")
Lstrfilename=replace (Lstrfilename, "", "")
' Lstrfilename=lstrfilename+ '. "
Lstrfilename=lstrfilename & Right (Path.getfilename (lstrFileName111), Len (Path.getfilename (lstrFileName111))-( InStr (Path.getfilename (lstrFileName111), ".")) +1)
Else
Lstrfilename = Path.getfilename (lstrfilename)
End If
' Remove directory information, return file name
' To determine whether an uploaded directory exists or not, to establish
If (not directory.exists (Lstrfilefolder)) Then
Directory.CreateDirectory (Lstrfilefolder)
End If
' Uploading files to the server
Lstrfilenamepath = Lstrfilefolder & Lstrfilename
lstrfilenamepath_1 = lstrfilefolder_1 & Lstrfilename
' Get uploaded directory and file name
LoFile.PostedFile.SaveAs (Lstrfilenamepath)
' Get and display the properties of the uploaded file
Filename.text = Lstrfilename
' Get file name
Filetype.text = LoFile.PostedFile.ContentType
' Get file type
Filelength.text = CStr (loFile.PostedFile.ContentLength)
' Get file length
Fileuploadform.visible = False
Answermsg.visible = True
' Show upload file properties
Addtodb ()
End Sub
' Add record information to the database, you can not do this step.
Sub Addtodb ()
Dim MyConnection As SqlConnection = New SqlConnection (configurationsettings.appsettings ("ConnStr"))
Dim insertcmd as String = "INSERT into Chaoqi (Title,filepath) VALUES (@title, @filepath)"
Dim mycommand as SqlCommand = new SqlCommand (Insertcmd, MyConnection)
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@title", SqlDbType.NVarChar, 50))
Mycommand.parameters ("@title"). Value = Title.value
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@filepath", SqlDbType.NVarChar, 50))
Mycommand.parameters ("@filepath"). Value = lstrfilenamepath_1
MyCommand.Connection.Open ()
Try
Mycommand.executenonquery ()
message.innerhtml = "<b> added record </b><br>" & Insertcmd.tostring ()
Catch Exp as SQLException
If Exp.number = 2627
message.innerhtml = "Error: Record with same primary key already exists"
Else
message.innerhtml = "Error: Failed to add record, make sure fields are filled in correctly"
End If
Message.style ("color") = "Red"
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service