从sql数据库将图像保存在本地文件夹中 [英] Save image in local Folder from sql database

查看:97
本文介绍了从sql数据库将图像保存在本地文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想根据图像名称从sql数据库中检索图像,并将图像存储在名为 图像的本地文件夹中 的。我可以从sql数据库中检索图像但我不知道如何创建名为Images的新文件夹并将我提取的图像存储在本地文件夹中。



谢谢和问候,



Dhinesh kumar V

Hi All,

I wants to retrieve images from sql database based on image name and store that images in local folder named as Images. I can retrieve images from sql database but i dont know how to create new folder named as Images and store my extracted image at local folder.

Thanks and Regards,

Dhinesh kumar V

推荐答案

这里:http://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory.aspx [<一个href =http://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory.aspxtarget =_ blanktitle =New Window> ^ ]。br />


我不知道如何存储图像名称。我希望您的数据库中有一列用于携带此信息。此外,如果这些名字不是唯一的,你应该做点什么。



如果你停止使用用户级的单词folder,也许你的生活会更轻松但是用户级隐喻,并接受编程级术语目录。



-SA
Here: http://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory.aspx[^].

I don't know how you store the image names though. I hope you have a column in your database to carry this information. Also, you should do something if those names are no unique.

Perhaps your life will be easier if you stop using user-level word "folder", which is nothing but a user-level metaphorе, and embrace the programming-level term "directory".

—SA


嗨...

!st为你的项目创建一个Images文件夹。然后

图像保存在文件夹中,其路径插入数据库中。

在aspx中

Hi...
!st create a Images folder to ur project. Then
for images are saved in folder abd its path insertered in database.
In aspx:
<asp:FileUpload ID="imgful" runat="server" Height="25px" style="font-weight: 700" />
<asp:button id="btnimg" runat="server" text="SaveProduct" onclick="btnimg_Click" xmlns:asp="#unknown"> ForeColor="DeepPink" style="font-weight: 700" Width="99px" Height="28px"/> </asp:button>



在aspx.cs


In aspx.cs:

protected void btnimg_Click(object sender, EventArgs e)
 {
   //Get Filename from fileupload control
                string filename = Path.GetFileName(imgful.PostedFile.FileName);
                //Save images into Images folder
                imgful.SaveAs(Server.MapPath("~/Images/" + filename));
                //Open the database connection
                con = new MySqlConnection(cs);
                con.Open();
cmd = new MySqlCommand("Insert into Items(ProductName,ImageUrl) values(@ImageName,@ImagePath)", con);
                        //Passing parameters to query
                        cmd.Parameters.AddWithValue("@ImageName", filename);
                        cmd.Parameters.AddWithValue("@ImagePath", "~/Images/" + filename);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        //Close dbconnection
                        con.Close();
}



用于重拍图像并在gridview中显示

在button_click


for retriving images and am displaying in gridview
In button_click:

con = new MySqlConnection(cs);
            con.Open();
            //Query to select images path and name from database
            cmd = new MySqlCommand("select * from Items", con);
            cmd.ExecuteNonQuery();
            da = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();

            gdimg.DataSource = ds;
            gdimg.DataBind();
            gdimg.Visible = true;



可能对你有用。谢谢你。


May its useful to u. Thank u.


protected void btn_submit_Click(object sender, EventArgs e)
   {
        if (FileUpload1.HasFile)
       {
           if (CheckExtension(FileUpload1.FileName))
           {
               if (FileUpload1.PostedFile.ContentLength < 10000000)
               {
                   String Path = Page.Server.MapPath("~/uploaded");
                   Path = Path + "\\" + FileUpload1.FileName;
                   FileUpload1.SaveAs(Path);


                   movie_detail objt = new movie_detail();
                   objt.Movie_Name = FileUpload1.FileName;
                   objt.Image_path = Path;
                   objt.Id =Convert.ToInt32(TextBox1.Text);
                   objt.label = txt_vtitle.Text;
                   objt.price = Convert.ToInt32(txt_date.Text);
                   objt.Categories = ddl_category.SelectedItem.Text;
                   obj.movie_details.InsertOnSubmit(objt);
                   obj.SubmitChanges();
                   Label1.Style.Add(HtmlTextWriterStyle.Color, "Red");
                   Label1.Text = "movie detail saved succesfully";

               }
               else
               {
                   Label1.Style.Add(HtmlTextWriterStyle.Color, "Red");
                   Label1.Text = "file size must be lessthen 10 mb";
               }
           }
           else
           {
               Label1.Style.Add(HtmlTextWriterStyle.Color, "Red");
               Label1.Text = "file extension only .jpg is accepted ";
           }
       }
       else
       {
           Label1.Style.Add(HtmlTextWriterStyle.Color, "Red");
           Label1.Text = "plz select a movie image";
       }




       reset();
   }
   public void reset()
   {
       ddl_category.SelectedItem.Text = "";
       txt_vtitle.Text = "";
       TextBox1.Text = "";
       txt_date.Text = "";

   }


这篇关于从sql数据库将图像保存在本地文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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