zoukankan      html  css  js  c++  java
  • 上传图片到文件夹并显示在GridView控件里面

    http://aspsnippets.com/Articles/Upload-images-to-folder-and-display-uploaded-images-in-ASPNet-GridView-using-C-and-VBNet.aspx

    1. Html 页面

     <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" /><hr />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowHeader="false">
        <Columns>
            <asp:BoundField DataField="Text" />
            <asp:ImageField DataImageUrlField="Value" ControlStyle-Height="100" ControlStyle-Width="100" />
        </Columns>
    </asp:GridView>
        </div>
    View Code

    2. 添加命名空间

    using System.IO;
    using System.Collections.Generic;

    3. 上传文件按钮事件

       protected void btnUpload_Click(object sender, EventArgs e)
            {
                if (FileUpload1.HasFile)
                {
                    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Image/") + fileName);
                    Response.Redirect(Request.Url.AbsoluteUri);
                }
            }
    View Code


    4.Page_Load 事件--加载图片并显示在GridView中

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    string[] filePaths = Directory.GetFiles(Server.MapPath("~/Image/"));
                    List<ListItem> files = new List<ListItem>();
                    foreach (string filePath in filePaths)
                    {
                        string fileName = Path.GetFileName(filePath);
                        files.Add(new ListItem(fileName, "~/Image/" + fileName));
                    }
                    GridView1.DataSource = files;
                    GridView1.DataBind();
                }
            }
    View Code

     ----------------------------------------

    ---------------------------------------

    从数据库获取binary形式存储的文件([Image]是图片字段,以Binary形式存储)

    http://www.beansoftware.com/ASP.NET-Tutorials/Save-Read-Image-Database.aspx

    protected void btnDisplayImage_Click(object sender, EventArgs e)
            {
                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("select[Image] from SaveImageByBinary where id =1",conn);
                    byte[] img=(byte[])cmd.ExecuteScalar();
                    MemoryStream ms = new MemoryStream();
                    ms.Write(img, 0, img.Length);
    
                    Bitmap bt = new Bitmap(ms);
                    Response.ContentType = "image/jpeg";
                    //bt.Save(Response.OutputStream, ImageFormat.Jpeg);
                    string filename = Server.MapPath("~/Image/12456.jpeg");
                    bt.Save(Response.OutputStream, ImageFormat.Jpeg);
                }
            }
  • 相关阅读:
    linux通过源码安装nodejs
    设置npm的镜像源
    ubuntu手动安装PhantomJS
    h2数据库的简单使用
    xampp启动失败 Apache shutdown unexpectedly
    phpqrcode实现二维码(含图片)
    php 使用 rabbitmq
    rabbitMQ linux安装
    rabbitmq的web管理界面无法使用guest用户登录
    linux安装使用xdebug
  • 原文地址:https://www.cnblogs.com/songxia/p/4037940.html
Copyright © 2011-2022 走看看