C # extjs images and attachments are stored in the database in binary format. images are displayed and attachments are downloaded.

Source: Internet
Author: User

Page

<Ext: fileupload runat = "  Server  " Label = "  Attachment  " Id = "  Attachment  " Width = "  200px  " > </Ext: fileupload> <Ext: fileupload runat = "  Server " Label = "  Photo  " Id = "  Photo  " Width = "  200px  " > </Ext: fileupload>

Add background attachments

 If (System. Io. Path. getextension (attachment. filename). tolower ()! = Null & Amp; system. Io. Path. getextension (attachment. filename). tolower ()! =""  ){  String Attachmentname = System. Io. Path. getfilename (attachment. filename );  String Fileattachment = System. Io. Path. getextension (attachment. filename). tolower ();  String [] Allowattachment = { "  . Jpg  " , "  . Gif  " ,"  . Docx  " , "  . Pptx  " , "  . Txt  " , "  . XSL  " , "  . Rar  " , "  . Doc " , "  . Zip  " , "  . PNG  " , "  . JPEG  " , "  . BMP  "  };  Bool BL = False ;  For ( Int I = 0 ; I <allowattachment. length; I ++ ){  If (Fileattachment = Allowattachment [I]) {BL = True  ;  Break  ;}}  If (!BL) {alert. showintop (  "  Upload an attachment in the correct format  "  );  Return  ;}  Int Attachmentlength = attachment. postedfile. contentlength; //  Body Size Byte [] filebytearrayatt = New Byte [attachmentlength]; //  Temporary storage of byte Arrays for image files Stream streamobjectatt = attachment. postedfile. inputstream; //  Create a data stream object  //  Reads image file data. filebytearray is the data storage body, 0 is the Data Pointer position, and filelne is the data length. Streamobjectatt. Read (filebytearrayatt, 0 , Attachmentlength ); //  Read the data from the 0th-Bit Array to the length of the array into the streamobjectatt. Hrmsfile. Attachment = Filebytearrayatt; hrmsfile. attachmentformat = Attachmentname;} // then save the new entity hrmsfile to the database. 

Attachment Download Page

 
<Table> <tr> <TD> <strong> Appendix: </strong> </TD> <a href ="Fujianxiazhai. ashx? Id = <% # eval ("ID") %>"> <% # Eval ("Attachmentformat") %> </A> </TD> </tr> </table>

Fujianxiazhai. ashx page

  String Id = context. Request. querystring [ " ID  "  ]; Hdqy. hdqyms. Business. mshrms. hrms_filebll BLL = New  Hdqy. hdqyms. Business. mshrms. hrms_filebll (); hdqy. hdqyms. entity. hrmsfile Ra = Bll. gethrmsfilebyid (ID );  If (Ra! = Null & RA. Attachment! = Null  ) {Context. response. Clear ();  String Strfilename =RA. attachmentformat; context. response. contenttype = "  Application/OCTET-STREAM  "  ; Context. response. addheader (  "  Content-Disposition  " , "  Attachment; filename =  " + Httputility. urlencode (RA. attachmentformat); context. response. Buffer = True ; Context. response. binarywrite (RA. Attachment); context. response. Flush (); context. response. Close ();} 

The photo upload and attachment are the same. The page displayed at the front-end is:

<TD rowspan ="4"Style ="Width: 100px;"> <IMG alt ="Personal photo"Height ="120px"Width ="100px"Src ='Handler. ashx? Id = <% # eval ("ID") %>'> </TD>

The same is to request an image from an ashx file.

 

     Public  Void  Processrequest (httpcontext context) {context. response. contenttype = "  Text/plain  "  ;  String Id = context. Request. querystring [ "  ID  "  ];  If ( String  . Isnullorempty (ID) {system. Io. filestream FS = New System. Io. filestream (system. Web. httpcontext. Current. server. mappath ( "  Http://www.cnblogs.com/icon/20111130160358207106.png  "  ), System. Io. filemode. Open, system. Io. fileaccess. Read );  Byte [] Image = New   Byte  [Fs. Length]; FS. Read (image,  0  , Image. Length); context. response. contenttype = " PNG  " ; //  Set the output file type Context. response. outputstream. Write (image, 0  , Image. Length); context. response. End ();  Return  ;} Hdqy. hdqyms. Business. mshrms. hrms_filebll BLL = New  Hdqy. hdqyms. Business. mshrms. hrms_filebll (); hdqy. hdqyms. entity. hrmsfile = Bll. gethrmsfilebyid (ID ); If (Hrmsfile! = Null & Hrmsfile. Photo! = Null & Hrmsfile. Photo. length! = 0  ) {Context. response. contenttype = Hrmsfile. photoformat; //  Set the output file type Context. response. outputstream. Write (hrmsfile. photo, 0  , Hrmsfile. Photo. Length); context. response. End ();}  Else {System. Io. filestream FS = New System. Io. filestream (system. Web. httpcontext. Current. server. mappath ( "  Http://www.cnblogs.com/icon/20111130160358207106.png  "  ), System. Io. filemode. Open, system. Io. fileaccess. Read );  Byte [] Image = New   Byte  [Fs. Length]; FS. Read (image,  0 , Image. Length); context. response. contenttype = "  PNG  " ; //  Set the output file type Context. response. outputstream. Write (image, 0  , Image. Length); context. response. End () ;}// the default image 20111130160358207106.png is provided when no image is available. 

Although it is better to store a path in the database, the requirements of the leaders have been fulfilled. Here, let's take a look at it first.

 

 

 

 

 

 

 

 

 

 

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.