Today, I met a project on an enterprise website. The customer asked the product data table in the system to use the database in their company's ERP, the product images can also be changed (the ERP product images are stored in the IMAGE field). If there is no replaceable IMAGE, then we can directly read the source IMAGE inserted by the ERP in the product table. After finding the source IMAGE, we can find a feasible code to read the IMAGE field and display the IMAGE in the browser:
C # code
- Try
- {
- Byte [] imgData;
- ImgData = (byte []) dtbl_pd.Rows [0] ["yytp"]; // IMAGE field
- Int iLength = imgData. Length;
- // FileStream fs = new FileStream ("E: \ Project \ CompanyWebSite \ 1.gif", System. IO. fileMode. create, System. IO. fileAccess. write); // can be used to generate the actual file
- // Fs. Write (imgData, 0, iLength );
- // Fs. Close ();
- Response. OutputStream. Write (imgData, 0, iLength );
- }
- Catch (Exception ex)
- {
- }
The call is very simple. For example, if the above Code is located on the page named Img. aspx, you can insert:
In addition, it also provides an insert when reading data. It is feasible after testing.
Key code of InsertImage. aspx:
HTML code
- <Form enctype = "multipart/form-data" runat = "server" id = "Form1">
- <Table runat = "server" width = "700" align = "left" id = "Table1" cellpadding = "0" cellspacing = "0" border = "0">
- <Tbody>
- <Tr>
- <Td> upload an image (select the image you want to upload) </td>
- <Td>
- <Input type = "file" id = "UP_FILE" runat = "server" style = "Width: 320" accept = "text/*" name = "UP_FILE"/>
- </Td>
- </Tr>
- <Tr>
- <Td> file description (add upload image description, for example, author and source) </td>
- <Td>
- <Asp: TextBox RUNAT = "server" WIDTH = "239" ID = "txtDescription"/>
- </Td>
- </Tr>
- <Tr>
- <Td>
- <Asp: Label RUNAT = "server" ID = "txtMessage" FORECOLOR = "red"/>
- </Td>
- <Td>
- <Asp: Button ID = "Button1" RUNAT = "server" WIDTH = "239" onCLICK = "Button_Submit" TEXT = "UploadImage"/> </td>
- </Tr>
- </Tbody>
- </Table>
- </Form>
Key code of InsertImage. aspx. cs:
C # code
- Protected void Button_Submit (System. Object sender, System. EventArgs e)
- {
- HttpPostedFile UpFile = UP_FILE.PostedFile; // HttpPostedFile object, used to read image file attributes
- FileLength = UpFile. ContentLength; // record file length
- Try
- {
- If (FileLength = 0)
- {// The file length is zero.
- TxtMessage. Text = "<B> select the file you want to upload </B> ";
- }
- Else
- {
- Byte [] FileByteArray = new Byte [FileLength]; // temporary storage of Byte Arrays for image files
- Stream StreamObject = UpFile. InputStream; // create a data Stream object
- // Read image file data. FileByteArray is the data storage body, 0 is the Data Pointer position, and filelne is the data length.
- StreamObject. Read (FileByteArray, 0, FileLength );
- // Create an SQL Server Link
- SqlConnection Con = new SqlConnection ("Data Source = Localhost; Initial Catalog = CompanyWebSiteData; User ID = sa; Pwd = chenfeng ;");
- String SqlCmd = "Insert INTO cp (cpbh, yytp) valueS (@ cpbh, @ Image )";
- SqlCommand CmdObj = new SqlCommand (SqlCmd, Con );
- CmdObj. Parameters. Add ("@ Image", SqlDbType. Binary, FileLength). Value = FileByteArray;
- CmdObj. Parameters. Add ("@ cpbh", SqlDbType. VarChar, 200). Value = txtDescription. Text;
- //// Upload the data records of other single tables
- // CmdObj. Parameters. Add ("@ ImageDescription", SqlDbType. VarChar, 200). Value = txtDescription. Text;
- //// Record the file length, which is used for reading
- // CmdObj. Parameters. Add ("@ ImageSize", SqlDbType. BigInt, 8). Value = UpFile. ContentLength;
- Con. Open ();
- CmdObj. ExecuteNonQuery ();
- Con. Close ();
- TxtMessage. Text = "<p> <B> OK! You have successfully uploaded your image </B> "; // a message indicating that the image has been uploaded successfully
- }
- }
- Catch (Exception ex)
- {
- TxtMessage. Text = ex. Message. ToString ();
- }
- }