关于在我的Web应用程序中上传图片. [英] About image upload in my web application.

查看:81
本文介绍了关于在我的Web应用程序中上传图片.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在asp.net中开发应用程序.当我通过浏览按钮选择任何图像时,我想在应用程序中显示图像.因此,请给我完整的过程和代码,在C#.net中进行图像上载并将其存储到gridview和sql server 2005中.

I m developing the application in asp.net. And I want to display image in my application when i select any image by browse button. So plz give me complete procedure and code in C#.net to image upload and store it into gridview and sql server 2005

推荐答案

图像将存储在数据库中,您可以将数据库与gridview绑定
要上传图片,请使用FileUpload Control
The image will be store in database and you can bind the database with gridview
to upload an image take FileUpload Control
protected void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {


            string filename = FileUpload1.FileName;
            string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
            if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".bmp" || fileExt == ".JPG" || fileExt == ".GIF" || fileExt == ".png" || fileExt == ".jpeg")
            {
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~\\ThoughtForDay\\" + filename));
                string path = "~\\ThoughtForDay\\" + filename;
                SqlCommand cmd = new SqlCommand("Insert into ThoughtFortheDay(Imageurl,date1,date2) values('" + path + "',@date1,@date2)", cnn);
                cmd.Parameters.AddWithValue("Imageurl",fileExt);
                cmd.Parameters.AddWithValue("date1", txtdate1.Text);
                cmd.Parameters.AddWithValue("date2", txtdate2.Text);
                cmd.CommandType = CommandType.Text;
                cnn.Open();
                cmd.ExecuteNonQuery();
                lblinfo.Text = "Uploaded Successfully";
                cnn.Close();
                
            }
            else
            {
                lblinfo.Text = "Browse Only jpg,gif,bmp,png,jpeg Image File!";
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    
    }


ThoughtForDay是图像的名称.
在gridview中显示图像,请参见
在GridView中显示图像
在GridView中播放图像


ThoughtForDay is foledr name for images.
to show image in gridview see
Displaying Images in a GridView
isplaying Images in a GridView


也可以尝试


这篇关于关于在我的Web应用程序中上传图片.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆