Asp.net data is directly output as download

Source: Internet
Author: User

 

Sometimes we want the dynamically generated data to be directly output as a file download, or open it directly with a software. The client displays the File Download Dialog Box. How can we do this, in fact, we can modify the response parameter and output the data directly.

 

Let's look at an example.

 

Private sub exportdown (byval info as string) 'output to the download stream
Dim filename as string = request. servervariables ("http_host") + "mysql. SQL"
Response. contenttype = "application/unknown"
Response. addheader ("content-disposition", "attachment; filename =" + filename)
Response. Write (Info)
Response. End ()

End sub

 

For common output file formats, refer to the following:

 

 

 

Response. contenttype type

'Ez '=> 'application/Andrew-inset ',
'Hqx' => 'application/mac-binhex40 ',
'Cpt' => 'application/MAC-compactpro ',
'Doc' => 'application/msword ',
'Bin' => 'application/octet-stream ',
'Dms '=> 'application/octet-stream ',
'Lha' => 'application/octet-stream ',
'Lzh '=> 'application/octet-stream ',
'Exe '=> 'application/octet-stream ',
'Class' => 'application/octet-stream ',
'So' => 'application/octet-stream ',
'Dll '=> 'application/octet-stream ',
'Oda '=> 'application/oda ',
'Pdf '=> 'application/pdf ',
'Ai' => 'application/postscript ',
'Eps '=> 'application/postscript ',
'Ps '=> 'application/postscript ',
'Smi' => 'application/SMIL ',
'Smil '=> 'application/SMIL ',
'Mfa' => 'application/vnd. mfa ',
'Xls '=> 'application/vnd. MS-Excel ',
'Ppt '=> 'application/vnd. MS-PowerPoint ',
'Wbxml' => 'application/vnd. WAP. wbxml ',
'Wmlc' => 'application/vnd. WAP. wmlc ',
'Wmlsc '=> 'application/vnd. WAP. wmlscriptc ',
'Bcpio' => 'application/X-bcpio ',
'Vcd' => 'application/X-cdlink ',
'Pgn' => 'application/X-chess-pgn ',
'Cpio' => 'application/X-cpio ',
'Csh' => 'application/X-csh ',
'Dcr '=> 'application/X-ctor ',
'Dir' => 'application/X-ctor ',
'Dxr' => 'application/X-ctor ',
'Dvi '=> 'application/X-DVI ',
'Spl' => 'application/X-futuresplash ',
'Gtar '=> 'application/X-gtar ',
'Hdf' => 'application/X-hdf ',
'Js' => 'application/X-JavaScript ',
'Skp' => 'application/X-koan ',
'Skd' => 'application/X-koan ',
'Skt' => 'application/X-koan ',
'Skm' => 'application/X-koan ',
'Latex '=> 'application/X-latex ',
'Nc '=> 'application/X-netcdf ',
'Cdf' => 'application/X-netcdf ',
'Sh' => 'application/X-Sh ',
'Shar '=> 'application/X-Shar ',
'Swf '=> 'application/X-Shockwave-flash ',
'Sit '=> 'application/X-stuffit ',
'Sv4cpio '=> 'application/x-sv4cpio ',
'Sv4crc '=> 'application/x-sv4crc ',
'Tar '=> 'application/X-tar ',
'Tcl '=> 'application/X-tcl ',
'Tex '=> 'application/X-Tex ',
'Textinfo' => 'application/X-textinfo ',
'Text' => 'application/X-textinfo ',
'T' => 'application/X-troff ',
'Tr' => 'application/X-troff ',
'Roff' => 'application/X-troff ',
'Man '=> 'application/X-troff-man ',
'Me' => 'application/X-troff-me ',
'Ms' => 'application/X-troff-Ms ',
'Ustar' => 'application/X-ustar ',
'Src' => 'application/X-WAIS-source ',
'Xhtml '=> 'application/XHTML + xml ',
'Xht '=> 'application/XHTML + xml ',
'Zip' => 'application/zip ',
'Au '=> 'audio/basic ',
'Snd' => 'audio/basic ',
'Mid '=> 'audio/midI ',
'Midi '=> 'audio/midI ',
'Kar '=> 'audio/midI ',
'Mpga' => 'audio/MPEG ',
'Mp2' => 'audio/MPEG ',
'Mp3' => 'audio/MPEG ',
'Aif' => 'audio/X-AIFF ',
'Aiff '=> 'audio/X-AIFF ',
'Aifc '=> 'audio/X-AIFF ',
'M3u' => 'audio/X-mpegurl ',
'Ram '=> 'audio/X-PN-RealAudio ',
'Rm '=> 'audio/X-PN-realaudio ',
'Rpm '=> 'audio/X-PN-RealAudio-plugin ',
'A' => 'audio/X-realaudio ',
'Wav '=> 'audio/X-WAV ',
'Pdb' => 'chemical/X-pdb ',
'Xyz' => 'chemical/X-xyz ',
'Bmp '=> 'image/BMP ',
'Gif' => 'image/gif ',
'Ief' => 'image/ief ',
'Jpeg '=> 'image/JPEG ',
'Jpg '=> 'image/JPEG ',
'Jpe' => 'image/JPEG ',
'Png '=> 'image/PNG ',
'Tiff '=> 'image/tiff ',
'Tif '=> 'image/tiff ',
'Djvu '=> 'image/vnd. djvu ',
'Djv' => 'image/vnd. djvu ',
'Wbmp '=> 'image/vnd. WAP. wbmp ',
'Ras '=> 'image/X-CMU-raster ',
'Pnm '=> 'image/X-portable-anymap ',
'Pbm' => 'image/X-portable-bitmap ',
'Pgm '=> 'image/X-portable-graymap ',
'Ppm '=> 'image/X-portable-pixmap ',
'Rgb '=> 'image/X-RGB ',
'Xbm '=> 'image/X-xbitmap ',
'Xpm' => 'image/X-xpixmap ',
'Xwd '=> 'image/X-xwindowdump ',
'Gies' => 'model/iges ',
& Apos; IGES & apos; = & apos; model/IGES & apos ',
'Msh' => 'model/mesh ',
'Mesh '=> 'model/mesh ',
'Silo' => 'model/mesh ',
'Wrl' => 'model/jpa ',
'Jpa' => 'model/jpa ',
'Css '=> 'text/CSS ',
'Html' => 'text/html ',
'Htm '=> 'text/html ',
'Asc '=> 'text/plain ',
'Txt '=> 'text/plain ',
'Rtx' => 'text/richtext ',
'Rtf '=> 'text/RTF ',
'Sgml' => 'text/sgml ',
'Sgm' => 'text/sgml ',
'Tsv' => 'text/TAB-separated-values ',
'Wml' => 'text/vnd. WAP. wml ',
'Wmls' => 'text/vnd. WAP. wmlscript ',
'Etx' => 'text/X-setext ',
'Xsl '=> 'text/xml ',
'Xml' => 'text/xml ',
'Mpeg '=> 'video/MPEG ',
'Mpg' => 'video/MPEG ',
'Mpe' => 'video/MPEG ',
'Qt '=> 'video/quicktime ',
'Mov' => 'video/quicktime ',
'Mxu' => 'video/vnd. mpegurl ',
'Av' => 'video/X-msvideo ',
'Movi' => 'video/X-SGI-movi ',
'Ice '=> 'x-conference/X-cooltalk'

 
From: http://www.cnblogs.com/chenghm2003/archive/2008/10/19/1314703.html
 

Different contenttypes will affect the client's effect. The default contenttype is text/html, that is, the webpage format.
The Code is as follows:
<% Response. contenttype = "text/html" %>
<! -- # I nclude virtual = "/contenttype.html" -->

 

Displayed as a webpage,
<% Response. contenttype = "text/plain" %>
<! -- # I nclude virtual = "/sscript/contenttype.html" -->

Display the original HTML code.

The following are some common contenttypes:
GIF images
<% Response. contenttype = "image/GIF" %>
<! -- # I nclude virtual = "/myimage.gif" -->

JPEG images
<% Response. contenttype = "image/JPEG" %>
<! -- # I nclude virtual = "/myimage.jpeg" -->

Tiff Images
<% Response. contenttype = "image/tiff" %>
<! -- # I nclude virtual = "/myimage. Tiff" -->

Microsoft Word Document
<% Response. contenttype = "application/MSWord" %>
<! -- # I nclude virtual = "/myfile.doc" -->

RTF document
<% Response. contenttype = "application/rtf" %>
<! -- # I nclude virtual = "/myfile. rtf" -->

Microsoft Excel document
<% Response. contenttype = "application/X-excel" %>
<! -- # I nclude virtual = "/myfile.xls" -->

Microsoft PowerPoint document
<% Response. contenttype = "application/MS-PowerPoint" %>
<! -- # I nclude virtual = "/myfile. Pff" -->

PDF document
<% Response. contenttype = "application/pdf" %>
<! -- # I nclude virtual = "/myfileprohibited" -->

Zip document
<% Response. contenttype = "application/zip" %>
<! -- # I nclude virtual = "/myfile.zip" -->

Below is a more detailed contenttype

// --------------------- The following figure shows the image obtained from the resource file. And display
Public class getimage
{
Public static system. Drawing. Image getsrc (string name)
{
System. Resources. ResourceManager Rm = new ResourceManager ("classlibrary. resourcetestimg ",
System. reflection. Assembly. getexecutingassembly (); // resourcetestimg indicates the name of the resource file and classlibrary // indicates the namespace.
Return rm. GetObject (name) as system. Drawing. image;
}

}

Show Code
Protected void page_load (Object sender, eventargs E)
{

System. Drawing. Image IMG = classlibrary. getimage. getsrc ("_ 15958260 ");
// _ 15958260 indicates the name of the resource image displayed in the resourcetestimg. resx Resource Management window.

IMG. Save (response. outputstream, system. Drawing. imaging. imageformat. JPEG );
}

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.