天天看點

C# 檔案下載下傳時改變檔案名

檔案下載下傳時改變檔案名 

在項目中有時會出現下載下傳檔案的問題,伺服器上儲存的檔案也許是已GUID命名的檔案,但在下載下傳時,也許需要指定一個檔案名。我項目中就用到了,到這裡做下筆記:

首先在頁面上放一個<iframe>标簽:

<iframe id="ifrLoad" frame name="weidu" scrolling="no" width="100%" height="100%">

        </iframe>

再在js中寫如下方法:

function FileDownLoad(filePath, fileName) {

    document.getElementById("ifrLoad").setAttribute("src", encodeURI("FileDownLoad.aspx?filePath=" + filePath + "&fileName=" + fileName));

}

注意檔案名如果是中文要記得用:js 的encodeURI()方法進行編碼傳輸

再到FileDownLoad.aspx頁面背景代碼進行轉碼操作,直接貼代碼:

 public partial class FileDownLoad : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            string filePath = Request.Params["filePath"];

            string fileName = HttpUtility.UrlDecode(Request.Params["fileName"]);

            if (filePath != null)

            {

                BigFileDownload(fileName, filePath);

            }

        }

        /// <summary>

        /// 檔案下載下傳

        /// </summary>

        /// <param name="FileName">檔案名</param>

        /// <param name="FilePath">路徑</param>

        public void BigFileDownload(string FileName, string FilePath)

        {

            System.IO.Stream iStream = null;

            // Buffer to read 10K bytes in chunk:

            byte[] buffer = new Byte[10000];

            // Length of the file:

            int length;

            // Total bytes to read:

            long dataToRead;

            // Identify the file to download including its path.

            string filepath = System.Web.HttpContext.Current.Server.MapPath(FilePath);

            // Identify the file name.

            string filename = System.IO.Path.GetFileName(filepath);

            try

            {

                // Open the file.

                iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,

                            System.IO.FileAccess.Read, System.IO.FileShare.Read);

                // Total bytes to read:

                dataToRead = iStream.Length;

                Response.ContentType = "application/octet-stream";

                Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(FileName));//System.Text.UTF8Encoding.UTF8.GetBytes(FileName)

                // Read the bytes.

                while (dataToRead > 0)

                {

                    // Verify that the client is connected.

                    if (Response.IsClientConnected)

                    {

                        // Read the data in buffer.

                        length = iStream.Read(buffer, 0, 10000);

                        // Write the data to the current output stream.

                        Response.OutputStream.Write(buffer, 0, length);

                        // Flush the data to the HTML output.

                        Response.Flush();

                        buffer = new Byte[10000];

                        dataToRead = dataToRead - length;

                    }

                    else

                    {

                        //prevent infinite loop if user disconnects

                        dataToRead = -1;

                    }

                }

            }

            catch (Exception ex)

            {

                // Trap the error, if any.

                string message = ex.Message;

                this.Page.ClientScript.RegisterStartupScript(GetType(), "Message", "<script>alert('Error : " + message + "');</script>");

            }

            finally

            {

                if (iStream != null)

                {

                    //Close the file.

                    iStream.Close();

                }

            }

        }

 }

這樣當點選檔案清單進行下載下傳時:就會将以你傳入的檔案名作為下載下傳的檔案名

<td style="width: 60%; height: 25px">

    <a href="javascript:void(0)" target="_blank" rel="external nofollow"   οnclick="FileDownLoad('../File/aseaglnsdf234dkfauoiomsadfk.doc','新設立外商投資企業區域分布情況報告(國家工商行政管理總局).doc')">新設立外商投資企業區域分布情況報告(國家工商行政管理總局)</a>

http://www.cnblogs.com/joey0210/archive/2011/09/02/2163659.html

繼續閱讀