Various download file modes Response.BinaryWrite (byte[] doccontent); Response.WriteFile (System.IO.FileInfo DownloadFile. FullName); Response.Write (String html2excel);

Source: Internet
Author: User
Tags httpcontext rar urlencode

Assign a value to XLS via HTML and download the XLS file

First, this. Response.Write (SW. ToString ()); System.IO.StringWriter sw = new System.IO.StringWriter ();

           this. Response.Clear ();
            string strFileName;
            strFileName =  "report" + ". xls";
            Response.Buffer = true;
            Response.ContentType = "application/ X-zip-compressed ";
            Response.AddHeader ("Accept-language", " ZH-CN ");
            Response.AddHeader ("Content-disposition" , "Attachment;filename=" + HttpContext.Current.Server.UrlEncode (strFileName));

            System.IO.StringWriter SW = new System.IO.StringWriter ();
            System.Web.UI.HtmlTextWriter htmlwrite = New HtmlTextWriter (SW);

            this.gvMutiQueResult.AllowPaging = false;
            if (session["QueryResult"]! = NULL)
            {
                 This.gvMutiQueResult.DataSource = (DataTable) session[" QueryResult "];
                This.gvMutiQueResult.DataBind ();
           }
            Else
             {
                 BINDGV (); Bind data
           }

This.gvMutiQueResult.RenderControl (Htmlwrite);
This . Response.Write (SW. ToString ());
This. Response.End ();
Response.Write ("<script type= ' Text/javascript ' >window.history.back ();</script>");

Second,Response.Write (html2excel); string html2excel

String strhtml; String FileName;

Response.Clear ();
Response.AddHeader ("Content-disposition", "attachment;filename=\" "+ httputility.urlencode (filename) +". Xls\ "");
Response.ContentType = "Application/ms-excel";

String html2excel = "+
String. Format (@ "<!--[if GTE MSO 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>{0}</x:Name>
<x:WorksheetOptions>
<x:Print>
<x:validprinterinfo/>
</x:Print>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml>
<! [endif]--> ", FileName)
+ "+ strHTML +
"</body>

Response.Write (html2excel);
Response.End ();

Three

//Get Data
                Idatabase SDB = Dbfactory.getdbinstance ();
                DataTable dt = Sdb.getdatatable ("SELECT * from  table where id= '" + ID.) ToString () + "'");
               //Output
                 if (dt. Rows.Count > 0)
                {

String docfilename = dt. rows[0]["Docfilename"]. ToString ();
String docsize = dt. rows[0]["DocSize"]. ToString ();
byte[] doccontent = (byte[]) dt. rows[0]["Doccontent"];

Response.Clear ();
Response.ClearHeaders ();
Response.Buffer = false;
String ContentType = dt. rows[0]["DocType"]. ToString ();
Response.ContentType = "Application/octet-stream";
Response.AddHeader ("Content-disposition", "attachment; Filename=\ "" + Httputility.urlencode (Docfilename, System.Text.Encoding.UTF8) + "\" ");
Response.AddHeader ("Content-length", docsize.tostring ());
Response.BinaryWrite (doccontent);
Response.Flush ();
Response.End ();

}

Iv.Response.WriteFile (downloadfile.fullname); System.IO.FileInfo DownloadFile = new System.IO.FileInfo (strFileName);

Download Boot reminder installer rar
private void Downloaddocument ()
{
Try
{
String strFileName = Server.MapPath (".. /.. /help/bootalert.rar ");
System.IO.FileInfo DownloadFile = new System.IO.FileInfo (strFileName);
Response.Clear ();
Response.ClearHeaders ();
Response.Buffer = false;
Response.ContentType = "Application/ms-excel";
Response.appendheader ("Content-disposition", "attachment;filename=" + httputility.urlencode (StrFileName, SYSTEM.TEXT.ENCODING.UTF8));
Response.appendheader ("Content-length", DownloadFile.Length.ToString ());
Response.WriteFile (downloadfile.fullname);
Response.Flush ();
Response.End ();
}
catch (Exception err)
{
throw err;
}
}

V. byte[] binarycontent response.binarywrite (binarycontent);

NewFileName = Newfilename.replace ("\\\\", "\ \"). Replace ("\ \", "/");
String outputfilename = newfilename.substring (Newfilename.lastindexof ('/') + 1);
byte[] binarycontent = System.IO.File.ReadAllBytes (NewFileName);
Response.Clear ();
Response.ClearHeaders ();
Response.Buffer = false;
Response.ContentType = "Application/octet-stream";
Response.AddHeader ("Content-disposition", "attachment; Filename=\ "" + Httputility.urlencode (OutputFileName, System.Text.Encoding.UTF8) + "\" ");
Response.AddHeader ("Content-length", Binarycontent.count (). ToString ());
Response.BinaryWrite (binarycontent);
Response.Flush ();
Response.End ();

Six,response.transmitfile (strFilePath, 0, fileSize); long fileSize = info. Length

FileInfo info = new FileInfo (strFilePath);
Long fileSize = info. Length;
Response.Clear ();
Response.Buffer = true;
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader ("Accept-language", "ZH-CN");
Response.AddHeader ("Content-disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode (info. Name));
Does not indicate that content-length with flush will not show download progress
Response.AddHeader ("Content-length", filesize.tostring ());
response.transmitfile (strFilePath, 0, fileSize);
Response.Flush ();
Response.close ();
Info. Delete ();

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.