Nov 22, 2011

How to give Link for Download or code for uploding and downloading file.

=========================== Upload Code ============================

 String strFileName = "", strFileExtension, strSavedFilePath;

            if (FileUploadCtd.HasFile)
            {
                strFileName = FileUploadCtd.FileName;
                strFileExtension = strFileName.Substring(strFileName.LastIndexOf('.') + 1);
                //if (strFileExtension.ToUpper() != "XLS")
                //{
                //    ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('Select Excel File Only.')</script>");
                //    return;
                //}
                strSavedFilePath = Server.MapPath("../CTD_Files");
                if (!Directory.Exists(strSavedFilePath))
                {
                    Directory.CreateDirectory(strSavedFilePath);
                }
                strFileName = strFileName.Substring(0, strFileName.LastIndexOf('.')) + DateTime.Now.ToString("ddMMyyyyhhmmss") + "." + strFileExtension.Trim();
                //strSavedFilePath += "\\" + strFileName.Substring(0, strFileName.LastIndexOf('.')) + DateTime.Now.ToString("ddMMyyyyhhmmss") + "." + strFileExtension.Trim();
                strSavedFilePath += "\\" + strFileName;
                FileUploadCtd.PostedFile.SaveAs(strSavedFilePath);

                objDesign._strvCTDPath = strFileName; // save in DB table.
                lblCTDLink.Text = strFileName;
            }
            else
            {
                objDesign._strvCTDPath = "";
                lblCTDLink.Text = "";
            }
            // CTD file uploading end Here

===============================================================

=========================On Page Load (Data Load) ====================

 // get file name from Db
lblCTDLink.Text = dtItemData.Rows[0]["vCTDPath"].ToString();

================================================================

======================= On download  link button click ====================

 protected void linkCTD_Click(object sender, EventArgs e)
    {
        // code for show donload link
        if (lblCTDLink.Text.Trim() != "")
        {
            string strSavedFilePath;
            strSavedFilePath = Server.MapPath("../CTD_Files" + "\\" + lblCTDLink.Text);
            FileInfo fileInfo = new FileInfo(strSavedFilePath);

            if (fileInfo.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.Flush();
                Response.WriteFile(fileInfo.FullName);
            }
        }
        else
        {
            objFnLib.WebMessageBox("No CTD Path Present", this);
        }
    }

================================================================

No comments:

Post a Comment

What is the use of n-tier architecture and 3-tier architecture?

how to implement 3-tier architecture in asp.net using c#. 3-Tier architecture is also called layered architecture. Some people called it ...