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