I,
Program Function: Upload is prohibited when the size of the uploaded image exceeds 8 K or the format is inconsistent. After the upload is successful, use the DataGrid to display the uploaded image.
2. Create a database
Create a new users table in the MSSQL northwind database. The table design is as follows:
| Column name |
Data Type |
Length |
Can it be blank? |
Others |
| ID |
Int |
4 |
No |
Primary Key, marked as yes, marked as seed 1, incrementing 1 |
| Headimg |
Varchar |
50 |
No |
|
Iii. Form Design:
1. Create an ASP. net web application, named datagrid3, and saved in the path http: // 192.168.0.1/datagrid3 (note: the IP address of the website on my machine is 192.168.0.1 and the home directory is D: \ Web Folder) click OK.
2. In the Solution Explorer window, rename webform1.aspx to uppicture. aspx, and then add a label control and a button to the form from the toolbox. then, add a file field control form interface to the form from an HTML toolbox as follows:
3. In the Solution Explorer window, right-click the project, select Add-new item-web form, and set the name to viewpicture. aspx. Then add a DataGrid Control in the opened form.
4. Right-click the DataGrid Control and click "Property generator" below to open the "DataGrid Property Window ". In the "DataGrid Properties window", click "column" to cancel the check box before "automatically create columns at runtime" and add a binding column to the selected column, enter "Serial Number" in the header text and ID in the data field. Add a binding column to the selected column, enter "Avatar" in the header text, and enter headimg in the data field. Click OK.
The form interface is as follows;
IV,CodeDesign:
1. uppicture. aspx
Imports system. Data. sqlclient
Public class webform1
Inherits system. Web. UI. Page
'Form Code omitted
'Upload an image
Private sub button#click (byval sender as system. Object, byval e as system. eventargs) handles button1.click
Dim IMG as string
'Define the postedfile file to store the File Uploaded by the user
Dim postedfile as httppostedfile = file1.postedfile
'Define a variable to store the size of the uploaded files.
Dim intimgsize as int32
'Get the size of the File Uploaded by the user,
Intimgsize = postedfile. contentlength
'If the file to be uploaded is not empty
If intimgsize <> 0 then
'Upload is prohibited if the value is greater than 8 KB.
If intimgsize> 8000 then
Label1.text = "the image is too large"
Exit sub
End if
'Define a variable to store the file type of the image uploaded by the user
Dim strimgtype as string = postedfile. contenttype
'Only Accept. GIF images
Dim filesplit () as string = Split (strimgtype ,"/")
Strimgtype = filesplit (filesplit. Length-1)
If strimgtype <> "GIF" then
Label1.text = "incorrect image format"
Exit sub
End if
'Store the entire path of the file to be uploaded
Filesplit = Split (postedfile. filename ,"\")
'Get the file name to upload
Dim filename as string = filesplit (filesplit. Length-1)
'Save the uploaded image to the headimg folder in the current directory of the server.
Postedfile. saveas (server. mappath ("headimg") & "\" & filename)
'Defines the path of the currently uploaded image on a variable Storage Server
Dim imgpath as string = "headimg \" & filename
IMG = "
'Store images in the database
Dim scon as new sqlconnection ("Server = localhost; database = northwind; uid = sa; Pwd = 123 ")
Scon. open ()
Dim scom as new sqlcommand ("insert into users values (@ IMG)", scon)
Scom. Parameters. Add ("@ IMG", sqldbtype. varchar). value = img
Try
Scom. executenonquery ()
Catch ex as exception
End try
Scon. Close ()
'Go To The image viewing window
Response. Redirect ("viewpicture. aspx ")
End if
End sub
End Class
|
2. viewpicture. aspx code:
imports system. data. sqlclient
public class viewpicture
inherits system. web. UI. page
'form Code omitted
private sub page_load (byval sender as system. object, byval e as system. eventargs) handles mybase. load
dim scon as new sqlconnection ("Server = localhost; database = northwind; uid = sa; Pwd = 123 ")
dim SDA as new sqldataadapter ("select * from users", scon)
dim ds as new dataset
try
SDA. fill (DS)
catch ex as exception
end try
datagrid1.datasource = DS
datagrid1.databind ()
end sub
end class |