Server we can save any type of file to SQL Server, and before we do that, we'll set up a test table, Testfile.sql:
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ TestFiles] ')
and OBJECTPROPERTY (ID, N ' isusertable ') = 1)
drop table [dbo]. [TestFiles]
Go
CREATE TABLE [dbo]. [TestFiles] (
[ID] [int] IDENTITY (1, 1) not NULL,
[myFileName] [varchar] (m) COLLATE chinese_prc_ci_as not NULL,
[FileType] [varchar] (m) COLLATE chinese_prc_ci_as not NULL,
[MyFile] [Image] Not NULL
) on [PRIMARY] textimage_on [PRIMARY]
Go
Create an upload form below:
Once the form is submitted, we use the PostedFile property of the HtmlInputFile class to access the file we uploaded, using the properties and methods of the Httppostedfile class to read, save the uploaded file, and obtain additional information about the uploaded file. Here we do not use the SaveAs method, because it is used to save the file. We're going to save the data to the database, we use the InputStream property, and it's used to initialize the stream to read our data. At the same time, we use ContentLength to read the file size, contenttype read the file type. It then creates a byte array, saves the file stream into the array, and then saves it to the database.
The following is the complete code "CS version" uploadfile.aspx:
Once we have uploaded successfully, we can browse the file: Just set the MIME type of the page and then output it with the BinaryWrite () of the response object.
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.