示例#1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                FilesBLL bll = new FilesBLL();
                Files f = new Files();
                int id;
                string filePath;
                string fileName;
                id = Convert.ToInt32(Request.QueryString["id"]);
                f.FUrl = bll.GetFileById(id).FUrl;
                f.FRName = bll.GetFileById(id).FRName;
                filePath = Server.MapPath(f.FUrl);//路径
                fileName = f.FRName;//客户端保存的文件名
                FileStream fs = new FileStream(filePath, FileMode.Open);
                byte[] bytes = new byte[(int)fs.Length];
                //以字符流的形式下载文件
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                Response.ContentType = "application/octet-stream";
                //通知浏览器下载文件而不是打开
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();

            }
            catch (IOException)
            {

                throw;
            }
        }
示例#2
0
 //修改图片信息
 //protected void photos_save_Click(object sender, EventArgs e)
 //{
 //    PhotosBLL bll = new PhotosBLL();
 //    Photos photos = new Photos();
 //    photos.P_Info = photos_info.Text;
 //    photos.P_Url = Common.photoid;
 //    if (bll.Modify(photos))
 //    {
 //        Response.Redirect(HttpUtility.UrlDecode(Request.QueryString["url"].ToString()));
 //    }
 //    else
 //    {
 //        ClientScript.RegisterStartupScript(GetType(), "", "alert('修改失败');", true);
 //    }
 //}
 /// <summary>
 /// 上传文件
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void btnfileload_Click(object sender, EventArgs e)
 {
     if (FileUpload.HasFile)
     {
         try
         {
             FilesBLL bll = new FilesBLL();
             Files file = new Files();
             string serverPath = Server.MapPath("/FileSave");
             if (!Directory.Exists(serverPath))
             {
                 Directory.CreateDirectory(serverPath);
             }
             string fileName = FileUpload.FileName;
             string newPath = serverPath + "\\" + fileName;
             file.FRName = fileName;
             FileUpload.SaveAs(newPath);
             file.FUrl = "FileSave/" + fileName;
             file.FSize = Convert.ToInt32(FileUpload.FileContent.Length / 1000);
             file.FUserId = Convert.ToInt32(cookieRead("id"));
             file.FName = file_info.Text;
             if (file.FName.Equals(""))
             {
                 file.FName = fileName;
             }
             bll.AddFiles(file);
             tagContent5.Style["display"] = "block";
             tagContent0.Style["display"] = "none";
             ClientScript.RegisterStartupScript(GetType(), "", "alert('上传成功');", true);
         }
         catch (Exception)
         {
             ClientScript.RegisterStartupScript(GetType(), "", "alert('上传失败');", true);
         }
     }
 }