zoukankan      html  css  js  c++  java
  • 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 = "文件上传成功";
                    
                }
            }
    
    
    
     
  • 相关阅读:
    (29)zabbix执行远程命令
    (28)zabbix用户宏变量详解macro
    CentOS7安装Nginx及配置
    Open-Falcon 监控系统监控 MySQL/Redis/MongoDB 状态监控
    Centos7安装ansible
    Centos7部署open-falcon 0.2
    Centos7安装redis
    vim常用命令
    CentOS7安装配置Bacula yum方法
    CentOS7时间同步
  • 原文地址:https://www.cnblogs.com/aswater-yuanye/p/3534982.html
Copyright © 2011-2022 走看看