• 文件上传实例


    文件上传实例

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using data;
    using dataDA;
    using System.IO;
    namespace web1
    {
    public partial class WebForm5 : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    
    }
    //上传到数据库image字段
    protected void Button1_Click(object sender, EventArgs e)
    {
    
    UploadData data = new UploadData();
    data.Owners = "路人甲";
    data.Name = data.Owners.ToString()+DateTime.Now.ToString("yyyyMMddhhmmssms")+ FileUpload1.FileName;
    data.Fil = FileUpload1.FileBytes ;
    new UploadDA().Insert(data);
    }
    //索引查看数据库图片
    protected void Button2_Click(object sender, EventArgs e)
    {
    UploadData data = new UploadDA().Select(Convert.ToInt16(TextBox1.Text));
    if (data != null)
    {
    FileStream fr = new FileStream(Server.MapPath("temp.jpg"), FileMode.Create);
    fr.Write(data.Fil, 0, data.Fil.Length);
    fr.Close();
    Image1.ImageUrl = "temp.jpg";
    
    }
    }
    //批量上传到硬盘
    protected void Button3_Click(object sender, EventArgs e)
    {
    try
    {
    int i = 0;
    foreach (Control ctl in form1.Controls)
    {
    if (ctl is FileUpload)
    {
    FileUpload up = ctl as FileUpload;
    i++;
    if (up.HasFile)
    {
    string path = Server.MapPath("img/"+DateTime.Now.ToString("yyyyMMddhhmmssms")+i.ToString("00")+up.FileName);
    up.SaveAs(path);
    }
    
    
    }
    }
    }
    catch
    {
    
    }
    
    
    }
    //显示图片
    protected void Button4_Click(object sender, EventArgs e)
    {
    Image1.ImageUrl = "img.aspx?id="+TextBox1.Text.Trim();
    }
    //单个保存到硬盘
    protected void Button6_Click(object sender, EventArgs e)
    {
    string path = Server.MapPath("img/" + DateTime.Now.ToString("yyyyMMddhhmmssms") + FileUpload1.FileName);
    FileUpload1.SaveAs(path);
    }
    //批量保存到数据库
    protected void Button5_Click(object sender, EventArgs e)
    {
    foreach (Control ctl in form1.Controls)
    {
    if (ctl is FileUpload)
    {
    FileUpload up = ctl as FileUpload;
    UploadData data = new UploadData();
    data.Owners = "路人甲";
    data.Name = data.Owners.ToString() + DateTime.Now.ToString("yyyyMMddhhmmssms") + up.FileName;
    data.Fil = up.FileBytes;
    new UploadDA().Insert(data);
    }
    }
    
    }
    }
    }
  • 相关阅读:
    UNIX网络编程(转载)
    cin、cin.get()、cin.getline()、getline()、gets()等函数的用法(转)
    GSL GNU Scientific Library
    为人处事很有意义
    上海老大杜月笙——教你看穿一个人
    超实用的Linux/Unix快捷键大汇总(开发、管理)(2)(转)
    STL map常用操作简介(转)
    使用ifstream和getline读取文件内容[c++]
    VIM Tips
    超实用的Linux/Unix快捷键大汇总(开发、管理)(1)(转)
  • 原文地址:https://www.cnblogs.com/lovesy2413/p/4546144.html
Copyright © 2020-2023  润新知