The Asp.net server calls the client script. The Asp.net server sends the file to the client. The value of reponse. contenttype is returned using outputstream. Write. The efficiency is writefi.

Source: Internet
Author: User

 

Server

 
Scriptmanager. registerstartupscript (This,This. GetType (),"Jsfunction","Clickaword ();",True);

 

Client:

 
<SCRIPT type = "text/jacascript">FunctionClickaword () {alert ("Hello World");
Document. getelementbyid ("Tempa"). Click (); // trigger the click event of Element A, cross-browser}</SCRIPT>

 

II. The Asp.net server sends the file to the client

Response. Redirect (filename); // from codeproject, whole englishsearch

 

3. Possible values of repsonse. contenttype

 <?  XML version = "1.0" encoding = "UTF-8"  ?>  <! --  Inernet media typeshttp: // en.wikipedia.org/wiki/internet_media_type  -->  <  Mediatypes  >     <  Mediatype  >       <  Contenttype  > Application/vnd. MS-Excel </  Contenttype  >        <  Name  > Microsoft Excel (TM) </  Name  >        <  Refurl  > Http://www.iana.org/assignments/media-types/application/vnd.ms-excel</  Refurl  >        <  Fileextensions  >           <  Fileextension  > . Xls </  Fileextension  >        </  Fileextensions  >     </ Mediatype  >     <  Mediatype  >        <  Contenttype  > Application/MSWord </  Contenttype  >        <  Name  > Microsoft Word </  Name >        <  Refurl  > Http://www.iana.org/assignments/media-types/application/msword </  Refurl  >        <  Fileextensions  >           <  Fileextension  > . Doc </  Fileextension >        </  Fileextensions  >     </  Mediatype  >     <  Mediatype  >        <  Contenttype  > Application/vnd. MS-PowerPoint </  Contenttype  >       <  Name  > Microsoft PowerPoint (TM) </  Name  >        <  Refurl  > Http://www.iana.org/assignments/media-types/application/vnd.ms-powerpoint </  Refurl  >        <  Fileextensions  >          <  Fileextension  > . Ppt </  Fileextension  >        </  Fileextensions  >     </  Mediatype  >     <  Mediatype  >        < Contenttype  > Application/PDF </  Contenttype  >        <  Name  > Portable Document Format </  Name  >        <  Refurl  > Http://www.iana.org/assignments/media-types/application/vnd.ms-powerpoint</  Refurl  >        <  Fileextensions  >           <  Fileextension  > . PDF </  Fileextension  >        </  Fileextensions  >     </ Mediatype  >     <  Mediatype  >        <  Contenttype  > Image/JPEG </  Contenttype  >        <  Name  > JPEG jfif Image </  Name >        <  Fileextensions  >           <  Fileextension  > . Jpg </  Fileextension  >           <  Fileextension  > . JPEG </  Fileextension  >       </  Fileextensions  >     </  Mediatype  >     <  Mediatype  >        <  Contenttype  > Image/GIF </  Contenttype  >        <  Name > GIF Image </  Name  >        <  Fileextensions  >           <  Fileextension  > . Gif </  Fileextension  >        </  Fileextensions  >    </  Mediatype  >     <  Mediatype  >        <  Contenttype  > Image/vnd. Microsoft. icon </  Contenttype  >        <  Name  > ICO Image </ Name  >        <  Refurl  > Http://www.iana.org/assignments/media-types/image/vnd.microsoft.icon </  Refurl  >        <  Fileextensions  >           <  Fileextension  > . ICO </ Fileextension  >        </  Fileextensions  >     </  Mediatype  >     <  Mediatype  >        <  Contenttype  > Application/zip </  Contenttype >        <  Name  > ZIP file </  Name  >        <  Refurl  > Http://www.iana.org/assignments/media-types/application/zip </  Refurl  >        <  Fileextensions  >          <  Fileextension  > . Zip </  Fileextension  >        </  Fileextensions  >     </  Mediatype  >  </  Mediatypes  > 

 

4. Using outputstream. Write to return a file is 10 times more efficient than writefile.

Txtreport: file path

  Byte [] Arbytefile = File. readallbytes (txtreport );  Using (Memorystream MS = New  Memorystream (arbytefile )){  Long Datalengthtoread = Ms. length;  Int Blocksize = datalengthtoread> = 5000 ? 5000 :( Int ) Datalengthtoread;  Byte [] Buffer = New   Byte  [Datalengthtoread]; context. response. Clear ();  //  Clear the content of the response  Context. response. clearcontent (); context. response. clearheaders ();  //  Buffer response so that page is sent  // After processing is complete. Context. response. bufferoutput = True  ;  //  Add the file name and attachment,  //  Which will force the open/cance/save dialog to show, to the header Context. response. addheader ( "  Content-Disposition  " , "  Attachment; filenamement import report .txt "  );  //  Bypass the open/save/cancel Dialog  //  Response. addheader ("content-disposition", "inline; filename =" + Doc. filename );  //  Add the file size into the Response Header Context. response. addheader ( "  Content-Length  "  , Arbytefile. length. tostring ());  // Set the contenttype Context. response. contenttype = "  Application/octet-stream  "  ;  //  Write the document into the response                  While (Datalengthtoread> 0 && Context. response. isclientconnected) {int32 lengthread = Ms. Read (buffer, 0 , Blocksize); context. response. outputstream. Write (buffer,  0  , Lengthread); context. response. Flush (); datalengthtoread = Datalengthtoread- Lengthread;} context. response. Flush (); context. response. Close ();} context. response. End (); 

//. Net Response Header

 

5. Comparison between download link click and outputstream;

Method 1: Server output. Stream. Write (byte []) // returns the object to be downloaded in binary stream

Method 2: The URI of the file returned by the server. After receiving the file by the client Ajax, <a id = "Tempa">; Tempa. href = URL; tema. Click (); downloads;

<A> In method 2 is called download link. Href = *. xls, *. Doc, *. Zip will pop up the download dialog box.

Href = *. txt, the content of the current page will be changed to TXT content. This is the limitation of downloadlink.

 

Related Article

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.