91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ASP.NET中下載文件的方法有哪些

發布時間:2021-03-08 14:11:04 來源:億速云 閱讀:162 作者:Leah 欄目:開發技術

這期內容當中小編將會給大家帶來有關ASP.NET中下載文件的方法有哪些,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

代碼如下:


  //TransmitFile實現下載
    protected void Button1_Click(object sender, EventArgs e)
    {
        /*
        微軟為Response對象提供了一個新的方法TransmitFile來解決使用Response.BinaryWrite
        下載超過400mb的文件時導致Aspnet_wp.exe進程回收而無法成功下載的問題。
        代碼如下:
        */
        Response.ContentType = "application/x-zip-compressed";
        Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
        string filename = Server.MapPath("DownLoad/z.zip");
        Response.TransmitFile(filename);
    }

    //WriteFile實現下載
    protected void Button2_Click(object sender, EventArgs e)
    {
        /*
        using System.IO;

        */
        string fileName = "asd.txt";//客戶端保存的文件名
        string filePath = Server.MapPath("DownLoad/aaa.txt");//路徑

        FileInfo fileInfo = new FileInfo(filePath);
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        Response.AddHeader("Content-Transfer-Encoding", "binary");
        Response.ContentType = "application/octet-stream";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.WriteFile(fileInfo.FullName);
        Response.Flush();
        Response.End();
    }

    //WriteFile分塊下載
    protected void Button3_Click(object sender, EventArgs e)
    {
        string fileName = "aaa.txt";//客戶端保存的文件名
        string filePath = Server.MapPath("DownLoad/aaa.txt");//路徑

        System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

        if (fileInfo.Exists == true)
        {
            const long ChunkSize = 102400;//100K 每次讀取文件,只讀取100K,這樣可以緩解服務器的壓力
            byte[] buffer = new byte[ChunkSize];

            Response.Clear();
            System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
            long dataLengthToRead = iStream.Length;//獲取下載的文件總大小
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
            while (dataLengthToRead > 0 && Response.IsClientConnected)
            {
                int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//讀取的大小
                Response.OutputStream.Write(buffer, 0, lengthRead);
                Response.Flush();
                dataLengthToRead = dataLengthToRead - lengthRead;
            }
            Response.Close();
        }
    }

    //流方式下載
    protected void Button4_Click(object sender, EventArgs e)
    {
        string fileName = "aaa.txt";//客戶端保存的文件名
        string filePath = Server.MapPath("DownLoad/aaa.txt");//路徑

        //以字符流的形式下載文件
        FileStream fs = new FileStream(filePath, FileMode.Open);
        byte[] bytes = new byte[(int)fs.Length];
        fs.Read(bytes, 0, bytes.Length);
        fs.Close();
        Response.ContentType = "application/octet-stream";
        //通知瀏覽器下載文件而不是打開
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
    }

//----------------------------------------------------------

public void DownloadFile( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FileBody )
{

  WebForm.Response.ClearHeaders();
  WebForm.Response.Clear();
  WebForm.Response.Expires = 0;
  WebForm.Response.Buffer = true;
  WebForm.Response.AddHeader("Accept-Language", "zh-tw");
  //'文件名稱
  WebForm.Response.AddHeader("content-disposition", "attachment; filename='"+System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8)+"'");
  WebForm.Response.ContentType = "Application/octet-stream";
  //'文件內容
  WebForm.Response.Write(FileBody);//-----------
    WebForm.Response.End();
}


//上面這段代碼是下載一個動態產生的文本文件,若這個文件已經存在于服務器端的實體路徑,則可以通過下面的函數:

public void DownloadFileByFilePath( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FilePath )
{
  WebForm.Response.ClearHeaders();
  WebForm.Response.Clear();
  WebForm.Response.Expires = 0;
    WebForm.Response.Buffer = true;
  WebForm.Response.AddHeader("Accept-Language", "zh-tw");
  //文件名稱
  WebForm.Response.AddHeader("content-disposition", "attachment; filename='" + System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8) +"'" );
  WebForm.Response.ContentType = "Application/octet-stream";
  //文件內容
  WebForm.Response.Write(System.IO.File.ReadAllBytes(FilePath));//---------
  WebForm.Response.End();
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

--保存文檔
string docName, docExtended;
Stream doc = fuDoc.FileContent;
int docLength = fuDoc.PostedFile.ContentLength;
byte[] docData = new byte[docLength];
doc.Read(docData, 0, docLength);
docExtended = fuDoc.FileName.Substring(fuDoc.FileName.IndexOf("."));
if (string.IsNullOrEmpty(tbDoc.Text))
docName = fuDoc.FileName;
else
docName = tbDoc.Text + docExtended;
SafetyDocInfo data = new SafetyDocInfo(ddlSort.Text, docName, fuDoc.PostedFile.ContentType, docData);
SafetyDoc safety = new SafetyDoc();
safety.Insert(data);
tbDoc.Text = string.Empty;
--打開文檔
public void ViewSafetyDoc(string pDocSort, string pDocName)
{
OracleParameter[] parms = GetSafetyDocParm(SQL_View_SafetyDoc);
parms[0].Value = pDocSort;
parms[1].Value = pDocName;
using (OracleDataReader rdr = OracleHelper.ExecuteReader(OracleHelper.ConnectionString, CommandType.Text, SQL_View_SafetyDoc, parms))
{
while (rdr.Read())
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = rdr.GetString(1);
HttpContext.Current.Response.BinaryWrite((byte[])rdr["Doc"]);
string FileName = rdr.GetString(0);
FileName = System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8);
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
}
}
}     

上述就是小編為大家分享的ASP.NET中下載文件的方法有哪些了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

兴安县| 胶南市| 隆化县| 巩留县| 台山市| 富平县| 馆陶县| 西宁市| 武功县| 微博| 芷江| 曲麻莱县| 石河子市| 天台县| 南华县| 文安县| 武宣县| 阿瓦提县| 松滋市| 边坝县| 红桥区| 白城市| 永川市| 新兴县| 云浮市| 元朗区| 永修县| 兴化市| 土默特左旗| 麦盖提县| 名山县| 连平县| 宣汉县| 柳江县| 扶余县| 桦甸市| 天祝| 屯昌县| 郑州市| 车致| 诸城市|