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 = "文件上传成功"; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。