ASP.NET 对于文件的下载与上传

 /// <summary>
        /// 下载附件查看
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void downButton_Command(object sender, CommandEventArgs e)
        {
//传递过来的参数
string fullName = e.CommandArgument.ToString(); string fileName=System.IO.Path.GetFileName(fullName); if (!string.IsNullOrEmpty(fullName)) { try { System.IO.FileInfo downloadFile = new System.IO.FileInfo(fullName); if (downloadFile.Exists) { Response.Clear(); Response.ClearHeaders(); Response.Buffer = false; Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.AppendHeader("Content-Length", downloadFile.Length.ToString()); Response.WriteFile(downloadFile.FullName); Response.Flush(); Response.End(); } else { ClientScript.RegisterStartupScript(this.GetType(), "", "alert('不存在这个链接')",true); } } catch { ClientScript.RegisterStartupScript(this.GetType(), "", "alert('操作失败')", true); } } }


 //文件的上传
 protected void appSubmit_Click(object sender, EventArgs e)
        {
            FileUpload upFile = (FileUpload)this.DVSalary.FindControl("appFileUpload");
            if (upFile.HasFile)
            {
                string staffId = ((Label)(this.DVSalary.FindControl("staffID"))).Text.Trim();
                string fileName = "";
                string[] strings = upFile.FileName.Split('\\');
                string[] docNames = strings[strings.Length - 1].Split('.');
                string docName = staffId + DateTime.Now.Year
                    + DateTime.Now.Month + DateTime.Now.DayOfYear 
                    + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second 
                    + DateTime.Now.Millisecond 
                    + "."
                    + docNames[docNames.Length - 1];
                fileName = "D:\\" + docName;
                upFile.SaveAs(fileName);
                //保存上传的附件名
                Session["file"] = fileName;
                Label tip = (Label)this.DVSalary.FindControl("toolTip");
                tip.Visible = true;
                tip.ForeColor = System.Drawing.Color.Red;
                tip.Text = "文件上传成功";
                
            }
        }

 

 

 

posted @ 2014-01-27 16:17  aswater  阅读(215)  评论(0编辑  收藏  举报