. Net could not open the Offie Open XML file (upload and download use)

Source: Internet
Author: User
Tags httpcontext urlencode

First, post an upload code:

1  Public Static stringUploadfileintodir (FileUpload MyFile,stringDirName)2         {3             if(Ifokfile (DirName) = =true)4             {5                 stringReturnstr =string.               Empty; 6                 if(MyFile.FileContent.Length >0)7                 {8Myfile.saveas (System.Web.HttpContext.Current.Request.MapPath (".. /uploadfile/") +DirName);9                     //writes the original file name and the current file name to the Erpsavefilename tableTen                     //string nowname = DirName; One                     //Modify the download file to the source file name + Time section A                     stringNowname =DirName; -                     stringOldname =Myfile.filename; -                     stringSqltempstr ="INSERT INTO Erpsavefilename (nowname,oldname) VALUES ('"+ Nowname +"', '"+ Oldname +"')"; the ZWL. DBUtility.DbHelperSQL.ExecuteSQL (SQLTEMPSTR); -                     returnoldname; -                 } -                 Else +                 { -                     returnReturnstr; +                 } A             } at             Else -             { -                 if(MyFile.FileName.Length >0) -                 { -System.Web.HttpContext.Current.Response.Write ("<script>alert (' Do not allow uploading of this type of file! ');</script>"); -                     return ""; in                 } -                 Else to                 { +                     return ""; -                 } the             } *}
View Code

The idea here is to upload a simple file to the server, just below the folder relative to the project path.

Then, post a download code:

1System.IO.Stream IStream =NULL;2         byte[] buffer =Newbyte[100000];3         intlength;4         LongdataToRead;5         stringfilepath =System.Web.HttpContext.Current.Server.MapPath (FilePath);6         stringfilename =System.IO.Path.GetFileName (filepath);7         Try8         {9IStream =NewSystem.IO.FileStream (filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read) ;TendataToRead =istream.length; OneResponse.ContentType ="Application/octet-stream"; AResponse.AddHeader ("content-disposition","attachment; Filename="+System.Web.HttpUtility.UrlEncode (FileName)); -              while(dataToRead >0) -             { the                 if(response.isclientconnected) -                 { -Length = istream.read (buffer,0,100000); -Response.OutputStream.Write (Buffer,0, length); + Response.Flush (); -Buffer =Newbyte[100000]; +dataToRead = dataToRead-length; A                 } at                 Else -                 { -dataToRead =-1; -                 } -             } -         } in         Catch(Exception ex) -         { to             stringMessage =Ex. Message; +              This. Page.ClientScript.RegisterStartupScript (GetType (),"Message","<script>alert (' Error:"+ Message +"');</script>"); -         } the         finally *         { $             if(IStream! =NULL)Panax Notoginseng             { - istream.dispose (); the             } +}
View Code

The code here is written because the data needs to be streamed.

  By the way to everyone a small download hint: When we save the file is afraid of the name of the file will use DateTime.Now.Ticks to prevent duplicate file name, but there is a distress when downloading, we look at a big push of the number, it is not clear which is which, then, can now build a separate page, then, this Page only this method, like the binary image we want to release, first set up a separate page, and then from the background of the binary transfer up the time to receive, and then another page to put the IMG tag, the binary page to put in, that is, we can give the file arbitrarily named.

  The theme is that there is a problem with the download here, the Office components under 03 and 03 can be arbitrary, but 07 of the components, because there is a backup file (especially docx), the download will appear when opened

Small partners do not worry, this problem I also met (this is click OK, and then continue to click Yes, or can get the original information), through two days of tangled, the root of the problem to find out, is a stream of ghosts, because the flow here although finally released, but because of backup, so the release is not complete So

Please use using{}

1 stringfilepath =System.Web.HttpContext.Current.Server.MapPath (FilePath);2         stringfilename =System.IO.Path.GetFileName (FilePath);3         using(varIStream =NewSystem.IO.FileStream (filepath, System.IO.FileMode.Open,4 System.IO.FileAccess.Read, System.IO.FileShare.Read))5         {6 response.clear ();7Response.ContentEncoding =System.Text.Encoding.UTF8;8Response.ContentType ="Application/octet-stream";9Response.AddHeader ("content-disposition","attachment; Filename="+Ten System.Web.HttpUtility.UrlEncode (FileName)); One Istream.copyto (response.outputstream); A Response.End (); -}
View Code

Or is the. Net framework design good, direct using, the memory of the people found directly released, the perfect solution

As for the usual file that cannot open the Office open Xml, please click here

. Net could not open the Offie Open XML file (upload and download use)

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.