We can save any type of file to SQL Server, before performing an example, set the test form, testfile.sql:
IF exists (select * from dbo.sysObjects where id = Object_id (n '[dbo]) And ObjectProperty (ID, n'susertable') = 1) Drop Table [dbo]. [Testfiles] Go
Create Table [DBO]. [TestFiles] ([Id] [INT] Identity (1, 1) Not Null, [MyFileName] [varchar] (50) collate chinese_prc_ci_as not null, [fileetype] [varchar] (50) Collate Chinese_PRC_CI_AS NOT NULL, [MyFile] NOT NULL) ON [PRIMARY] textImage_on [primary] Go
Create up to the uplist below:
Once the form is submitted, we use the HTMLINPUTFILE class PostedFile property to access the files we uploaded.
Use the attributes and methods of the HTTPPostedFile class to read, save upload files and get other information for upload files.
Here we don't use the SaveAs method because it is used to save the file. We have to save the data to the database,
We use the InputStream property that is used to initialize the stream to read our data. at the same time,
We use ContentLength to read the file size, and ContentType reads the file type. Then create a BYTE array,
Save the file into this array and save it to the database.
Here is the complete code [CS version] UPLOADFILE.ASPX:
<% @Page Language = "C #"%> <% @Import namespace = "system.io"%> <% @ Import name "%>" system.data "%> <% @ import namespace =" system.data.sqlclient " %>