Upload
Protected void btnupload_click (Object sender, eventargs e) {If (! Filupload. hasfile) {alert ("file not uploaded"); return;} string sreg = @ "\. jpg | \. JPEG | \. PNG"; if (! RegEx. ismatch (filupload. filename, sreg, regexoptions. ignorecase) {alert ("only images in JPG or PNG format are accepted"); return;} int filesize = filupload. postedfile. contentlength; If (filesize <= 0) {alert ("File Upload Failed"); return;} int K = filesize/1024; If (k> 300) {alert ("the file is too large"); return;} string filetype = filupload. postedfile. contenttype; string filename = path. getfilename (filupload. postedfile. filename); byte [] filebytearray; try {// temporary storage of image files byte array filebytearray = new byte [filesize]; // create a data stream to stream streamobject = filupload. postedfile. inputstream; // reads image file data. filebytearray is the data storage body, 0 is the Data Pointer position, and filesize is the Data Length streamobject. read (filebytearray, 0, filesize);} catch {alert ("File Upload Failed"); return;} sqlparameter [] sqlparas = new sqlparameter [5]; sqlparas [0] = new sqlparameter ("@ tpappid", tpappid); sqlparas [1] = new sqlparameter ("@ attachmentname", filename ); sqlparas [2] = new sqlparameter ("@ attachment", filebytearray); sqlparas [3] = new sqlparameter ("@ attachmentsize", k ); sqlparas [4] = new sqlparameter ("@ attachmentcontenttype", filetype); sqlhelper. dbhelper. executenonquery (commandtype. storedprocedure, "tp_prd_upload", sqlparas );}
// Display
Protected void page_load (Object sender, eventargs e) {sqlparameter [] sqlparas = new sqlparameter [1]; sqlparas [0] = new sqlparameter ("@ ID", convert. toint32 (request. querystring ["tpid"]); datatable dt = sqlhelper. dbhelper. executedatatable (commandtype. storedprocedure, "tp_prd_showupload", sqlparas); If (DT. rows. count = 0) return; datarow DR = DT. rows [0]; response. clear (); // set the output file type response. contenttype = Dr ["attachmentcontenttype"]. tostring (); // response. addheader ("content-disposition", "attachment; filename =" + Dr ["attachmentname"]. tostring () + ";"); // outputs the binary response of the image file. binarywrite (byte []) Dr ["attachment"]); response. flush (); response. end ();}
Appendix:
======================================
All types of Asp.net postedfile. contenttype are sorted alphabetically ". *" = "application/octet-stream"
". 001" = "application/x-001"
". 301" = "application/x-301"
". 323" = "text/h323"
". 906" = "application/x-906"
". 907" = "Fig/907"
". A11" = "application/x-a11"
". ACP" = "audio/X-Mei-AAC"
". Ai" = "application/postscript"
". AIF" = "audio/AIFF"
". Aifc" = "audio/AIFF"
". AIFF" = "audio/AIFF"
". Anv" = "application/X-anv"
". Asa" = "text/asa"
". ASF" = "Video/X-MS-ASF"
". Asp" = "text/asp"
". ASX" = "Video/X-MS-ASF"
". Au" = "audio/basic"
". Avi" = "Video/Avi"
". AWF" = "application/vnd. Adobe. Workflow"
". Biz" = "text/XML"
". BMP" = "application/X-BMP"
". Bot" = "application/X-bot"
". C4t" = "application/x-c4t"
". C90" = "application/x-c90"
". Cal" = "application/X-cals"
". Cat" = "application/vnd. ms-pki.seccat"
". CDF" = "application/X-netcdf"
". CDR" = "application/X-CDR"
". Cel" = "application/X-Cel"
". Cer" = "application/x-x509-ca-cert"
". Cg4" = "application/x-g4"
". CGM" = "application/X-CGM"
". Cit" = "application/X-CIT"
". Class" = "Java /*"
". CRF" = "text/XML"
". CMP" = "application/X-CMP"
". Cmx" = "application/X-cmx"
". Cot" = "application/X-cot"
". CRL" = "application/pkix-CRL"
". CRT" = "application/x-x509-ca-cert"
". CSI" = "application/X-CSI"
". CSS" = "text/CSS"
". Cut" = "application/X-cut"
". DBF" = "application/X-DBF"
". DBM" = "application/X-dBm"
". DBX" = "application/X-dbx"
". DCD" = "text/XML"
". Dcx" = "application/X-dcx"
". Der" = "application/x-x509-ca-cert"
". Dgn" = "application/X-dgn"
". Dib" = "application/X-dib"
". Dll" = "application/X-msdownload"
". Doc" = "application/MSWord"
". Dot" = "application/MSWord"
". Drw" = "application/X-drw"
". DTD" = "text/XML"
". DwF" = "model/vnd. DwF"
". DwF" = "application/X-DwF"
". DWG" = "application/X-DWG"
". Dxb" = "application/X-dxb"
". DXF" = "application/X-DXF"
". Edn" = "application/vnd. Adobe. edn"
". EMF" = "application/X-EMF"
". Eml" = "message/rfc822"
". Ent" = "text/XML"
". Epi" = "application/X-epi"
". EPS" = "application/X-Ps"
". EPS" = "application/postscript"
". ETD" = "application/X-EBX"
". EXE" = "application/X-msdownload"
". Fax" = "image/fax"
". FDF" = "application/vnd. FDF"
". Fif" = "application/Fractals"
". FO" = "text/XML"
". Frm" = "application/X-frm"
". G4" = "application/x-g4"
". GBR" = "application/X-GBR"
". GCD" = "application/X-GCD"
". GIF" = "image/GIF"
". Gl2" = "application/x-gl2"
". Gp4" = "application/x-gp4"
". Hgl" = "application/X-hgl"
". Hmr" = "application/X-hmr"
". HPG" = "application/X-HPGL"
". Hpl" = "application/X-hpl"
". Hqx" = "application/mac-binhex40"
". HRF" = "application/X-HRF"
". HTA" = "application/HTA"
". HTC" = "text/X-component"
". Htm" = "text/html"
". Html" = "text/html"
". Htt" = "text/webviewhtml"
". Htx" = "text/html"
". ICB" = "application/X-ICB"
". ICO" = "image/X-icon"
". ICO" = "application/X-ICO"
". IFF" = "application/X-Iff"
". Ig4" = "application/x-g4"
". Ig" = "application/X-ig"
". III" = "application/X-iPhone"
". IMG" = "application/X-IMG"
". INS" = "application/X-Internet-signup"
". ISP" = "application/X-Internet-signup"
". IVF" = "Video/X-IVF"
". Java" = "Java /*"
". Jfif" = "image/JPEG"
". Jpe" = "image/JPEG"
". Jpe" = "application/X-jpe"
". Jpeg" = "image/JPEG"
". Jpg" = "application/X-jpg"
". Js" = "application/X-JavaScript"
". Jsp" = "text/html"
". La1" = "audio/X-liquid-file"
". Lar" = "application/X-laplayer-Reg"
". Latex" = "application/X-latex"
". LAVS" = "audio/X-liquid-secure"
". LBM" = "application/X-LBM"
". Lmsff" = "audio/X-la-LMS"
". Ls" = "application/X-JavaScript"
". LTR" = "application/X-LTR"
". M1v" = "Video/X-mPEG"
". M2v" = "Video/X-mPEG"
". M3u" = "audio/mpegurl"
". M4e" = "Video/MPEG4"
". Mac" = "application/X-Mac"
". Man" = "application/X-troff-man"
". Math" = "text/XML"
". Mdb" = "application/msaccess"
". Mdb" = "application/X-mdb"
". MFP" = "application/X-Shockwave-flash"
". MHT" = "message/rfc822"
". MHTML" = "message/rfc822"
". Mi" = "application/X-MI"
". Mid" = "audio/mid"
". Midi" = "audio/mid"
". Mil" = "application/X-mil"
". MML" = "text/XML"
". MND" = "audio/X-MusicNet-download"
". MnS" = "audio/X-MusicNet-stream"
". Mocha" = "application/X-JavaScript"
". Movie" = "Video/X-SGI-movie"
". MP1" = "audio/MP1"
". MP2" = "audio/MP2"
". Mp2v" = "Video/MPEG"
". MP3" = "audio/MP3"
". MP4" = "Video/MPEG4"
". MPa" = "Video/X-MPG"
". MPD" = "application/vnd. MS-Project"
". MPE" = "Video/X-mPEG"
". MpEG" = "Video/MPG"
". Mpg" = "Video/MPG"
". Mpga" = "audio/RN-mPEG"
". MPP" = "application/vnd. MS-Project"
". MPS" = "Video/X-mPEG"
". MPT" = "application/vnd. MS-Project"
". MPV" = "Video/MPG"
". MPV2" = "Video/MPEG"
". MPW" = "application/vnd. MS-Project"
". MPX" = "application/vnd. MS-Project"
". CTX" = "text/XML"
". Mxp" = "application/X-mmxp"
". Net" = "image/pnetvue"
". NRF" = "application/X-NRF"
". Nws" = "message/rfc822"
". ODC" = "text/X-MS-ODC"
". Out" = "application/X-out"
". P10" = "application/pkcs10"
". P12" = "application/x-pkcs12"
". P7b" = "application/x-pkcs7-certificates"
". P7c" = "application/pkcs7-mime"
". P7m" = "application/pkcs7-mime"
". P7r" = "application/x-pkcs7-certreqresp"
". P7s" = "application/pkcs7-signature"
". Pc5" = "application/x-pc5"
". PCI" = "application/X-PCI"
". PCL" = "application/X-PCL"
". PCX" = "application/X-PCX"
". Pdf" = "application/pdf"
". Pdf" = "application/pdf"
". PDX" = "application/vnd. Adobe. PDX"
". Pfx" = "application/x-pkcs12"
". PGL" = "application/X-PGL"
". PIC" = "application/X-pic"
". PKO" = "application/vnd. ms-pki.pko"
". Pl" = "application/X-perl"
". PLG" = "text/html"
". Pls" = "audio/scpls"
". PLT" = "application/X-PLT"
". PNG" = "image/PNG"
". PNG" = "application/X-PNG"
". Pot" = "application/vnd. MS-PowerPoint"
". PPA" = "application/vnd. MS-PowerPoint"
". Ppm" = "application/X-ppm"
". PPS" = "application/vnd. MS-PowerPoint"
". Ppt" = "application/vnd. MS-PowerPoint"
". Ppt" = "application/X-ppt"
". PR" = "application/X-Pr"
". PRF" = "application/pics-rules"
". PRN" = "application/X-PRN"
". PRT" = "application/X-PRT"
". Ps" = "application/X-Ps"
". Ps" = "application/postscript"
". PTN" = "application/X-PTN"
". Pwz" = "application/vnd. MS-PowerPoint"
". R3t" = "text/vnd. rn-realtext3d"
". Ra" = "audio/vnd. Rn-RealAudio"
". Ram" = "audio/X-PN-RealAudio"
". RAS" = "application/X-ras"
". Rat" = "application/rat-file"
". RDF" = "text/XML"
". REC" = "application/vnd. Rn-recording"
". Red" = "application/X-red"
". RGB" = "application/X-RGB"
". RJS" = "application/vnd. Rn-RealSystem-RJS"
". Rjt" = "application/vnd. Rn-RealSystem-rjt"
". RLC" = "application/X-RLC"
". RLE" = "application/X-RLE"
". Rm" = "application/vnd. Rn-RealMedia"
". RMF" = "application/vnd. Adobe. RMF"
". Rmi" = "audio/mid"
". Rmj" = "application/vnd. Rn-RealSystem-rmj"
". RMM" = "audio/X-PN-RealAudio"
". RMP" = "application/vnd. rn-rn_music_package"
". RMS" = "application/vnd. Rn-RealMedia-secure"
". Rmvb" = "application/vnd. Rn-RealMedia-VBr"
". RMX" = "application/vnd. Rn-RealSystem-RMX"
". Rnx" = "application/vnd. Rn-RealPlayer"
". RP" = "image/vnd. Rn-realpix"
". RPM" = "audio/X-PN-RealAudio-plugin"
". Rsml" = "application/vnd. Rn-rsml"
". RT" = "text/vnd. Rn-RealText"
". Rtf" = "application/MSWord"
". Rtf" = "application/X-rtf"
". RV" = "Video/vnd. Rn-realvideo"
". Sam" = "application/X-Sam"
". Sat" = "application/X-sat"
". SDP" = "application/SDP"
". SDW" = "application/X-SDW"
". Sit" = "application/X-stuffit"
". SLB" = "application/X-SLB"
". SLD" = "application/X-SLD"
". SLK" = "Drawing/X-SLK"
". SMI" = "application/smil"
". Smil" = "application/smil"
". SMK" = "application/X-SMK"
". Snd" = "audio/basic"
". Sol" = "text/plain"
". Sor" = "text/plain"
". SPC" = "application/x-pkcs7-certificates"
". SPL" = "application/futuresplash"
". SPP" = "text/XML"
". SSM" = "application/streamingmedia"
". SST" = "application/vnd. ms-pki.certstore"
". STL" = "application/vnd. ms-pki.stl"
". Stm" = "text/html"
". Sty" = "application/X-Sty"
". SVG" = "text/XML"
". SWF" = "application/X-Shockwave-flash"
". TDF" = "application/X-TDF"
". TG4" = "application/x-tg4"
". TGA" = "application/X-TGA"
". Tif" = "image/tiff"
". Tif" = "application/X-TIF"
". Tiff" = "image/tiff"
". TLD" = "text/XML"
". Top" = "Drawing/X-top"
". Torrent" = "application/X-BitTorrent"
". TSD" = "text/XML"
". Txt" = "text/plain"
". Uin" = "application/X-ICQ"
". ULS" = "text/iuls"
". VCF" = "text/X-vCard"
". VDA" = "application/X-VDA"
". Vdx" = "application/vnd. Visio"
". VML" = "text/XML"
". VPG" = "application/x-vpeg005"
". Sealing" = "application/vnd. Visio"
". Sealing" = "application/X-sealing"
". Vss" = "application/vnd. Visio"
". VST" = "application/vnd. Visio"
". VST" = "application/X-VST"
". Vsw" = "application/vnd. Visio"
". Vsx" = "application/vnd. Visio"
". Vtx" = "application/vnd. Visio"
". VXML" = "text/XML"
". Wav" = "audio/WAV"
". Wax" = "audio/X-MS-wax"
". Wb1" = "application/x-wb1"
". Wb2" = "application/x-wb2"
". Wb3" = "application/x-wb3"
". Wbmp" = "image/vnd. WAP. wbmp"
". Wiz" = "application/MSWord"
". Wk3" = "application/x-wk3"
". Wk4" = "application/x-wk4"
". Wkq" = "application/X-wkq"
". Wks" = "application/X-wks"
". WM" = "Video/X-MS-WM"
". Wma" = "audio/X-MS-WMA"
". WMD" = "application/X-MS-WMD"
". WMF" = "application/X-WMF"
". WML" = "text/vnd. WAP. WML"
". Wmv" = "Video/X-MS-WMV"
". Wmx" = "Video/X-MS-wmx"
". Wmz" = "application/X-MS-WMZ"
". WP6" = "application/x-wp6"
". WPD" = "application/X-WPD"
". WPG" = "application/X-WPG"
". WPL" = "application/vnd. MS-WPL"
". Wq1" = "application/x-wq1"
". Wr1" = "application/x-wr1"
". WRI" = "application/X-WRI"
". Wrk" = "application/X-wrk"
". Ws" = "application/X-ws"
". WS2" = "application/X-ws"
". WSC" = "text/scriptlet"
". WSDL" = "text/XML"
". WVX" = "Video/X-MS-WVX"
". XDP" = "application/vnd. Adobe. XDP"
". XDR" = "text/XML"
". XFD" = "application/vnd. Adobe. XFD"
". XFDF" = "application/vnd. Adobe. XFDF"
". XHTML" = "text/html"
". Xls" = "application/vnd. MS-excel"
". Xls" = "application/X-xls"
". Xlw" = "application/X-xlw"
". Xml" = "text/XML"
". Xpl" = "audio/scpls"
". XQ" = "text/XML"
". Xql" = "text/XML"
". XQuery" = "text/XML"
". XSD" = "text/XML"
". XSL" = "text/XML"
". XSLT" = "text/XML"
". Xwd" = "application/X-xwd"
". X_ B" = "application/x-x_ B"
". X_t" = "application/x-x_t"
Reference: http://hi.baidu.com/dukey/item/6200ca16ab467f5bf0090eab