ASP. NET writes the file stream to a temporary file and opens

Source: Internet
Author: User

Today, a development task is encountered and a third-party interface needs to be tuned, and the interface returns a stream file stream (PDF file), which is then printed by the user.

The following is the source code:

  Public voidprintordertestforpdf () {//1. Create an entityPrintorderinfo model =NewPrintorderinfo () {ApiToken="XXX", Labelformat="A4_2", OutputFormat="PDF", Printcustoms=false, Printproduct=false, Printproductformat="{SKU}", OrderList=NewList<printorderdetail>()                {                    NewPrintorderdetail () {orderid="123456", trackingno=""}                }            }; //2. Request the interface and receive the returned file streamMemoryStream BTS =(MemoryStream) RUIYOUPAIDAL.INSTANCE.PRINTORDERFORPDF (model); //3. Write the file stream to a temporary PDF file and openProcess.Start (Gereratefilename (BTS)); } /// <summary>        ///writes the received file stream to a temporary file/// </summary>        /// <param name= "result" ></param>        /// <returns></returns>         Public Static stringgereratefilename (MemoryStream result) {varfilename = Getgeneratedfilename ();//1. Create a temporary file in. pdf format            varFS =NewFileStream (filename, filemode.openorcreate); varW =NewBinaryWriter (FS); W.write (result. ToArray ());//2. Write the received file stream to a temporary fileFS.            Close (); Result.            Close (); returnfilename; }        /// <summary>        ///Generate temporary Files/// </summary>        /// <returns></returns>         Public Static stringGetgeneratedfilename () {varGUID = Guid.NewGuid (). ToString (). Replace ("-",""); varfilepath = Path.Combine (Path.gettemppath (), GUID +". pdf"); returnfilepath; }

File stream operation is always easy to forget, only often review, often summed up ....

ASP. NET writes the file stream to a temporary file and opens

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.