图像从数据库上传和显示 [英] Image upload and display from database

查看:81
本文介绍了图像从数据库上传和显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据库中上传图片并希望在上传图片时在gridview bt中显示该图片即时出现问题。帮助我/删除错误的图片..



< br $> b $ b

 受保护  void  Button1_Click( object  sender,EventArgs e)
{
if ( FileUpload1.HasFile)
{
String str = FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(Server.MapPath( )+ 〜// uploadedimages // + str);
string path = 〜//上传// + str.ToString();
con.Open();
SqlCommand cmd = new SqlCommand( insert上传值(' + TextBox1.Text + ',' +路径+ '),con);
cmd.ExecuteNonQuery();
con.Close();
Label1.Text = 图片上传成功;
}
else
{
Label1.Text = Plz选择你的图像并上传;
}

}



当我使用path as =〜// uploadedimages //<时出现错误br $>


找不到路径'F:\Atverse \Web示例\图像\,〜\uploadedimages \Chrysanthemum.jpg'的一部分。





当我使用path as =~F:/ Atverse / Web Example / Image / uploadedimages /


不支持给定路径的格式。





plzz告诉我错误..或正确的代码。

解决方案

  string  
filename = Path.GetFileName(fileupload1.FileName);
fileupload1.SaveAs(Server.MapPath( 〜/
+ filename);



试一试,希望这会有所帮助。


< blockquote>在sql server management st中运行此脚本udio

  CREATE   TABLE  [dbo] 。[FileUploads](
[uid] [ int ] IDENTITY 1 1 NOT NULL
[uname] [ nvarchar ](max) NULL
[uimg] [ nvarchar ](max) NULL
ON [ PRIMARY ] TEXTIMAGE_ON [ PRIMARY ]



更改方法中的以下行:

 SqlCommand cmd =  SqlCommand( 插入FileUploads值(' + TextBox1.Text +  ',' + theFileName +  '),con); 





更新一个:

  protected   void  Button1_Click( object  sender,EventArgs e)
{
string connectionString = 将连接字符串放在此处;
if (FileUpload1.HasFile)
{
string theFileName = Path.Combine(Server.MapPath( 〜/ Uploads),FileUpload1.FileName);

使用 var con = new SqlConnection(connectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand( insert上传值(' + TextBox1.Text + ',' + theFileName + '),con);
cmd.ExecuteNonQuery();
}
if (File.Exists(theFileName))
{
File.Delete(theFileName);
}
FileUpload1.SaveAs(theFileName);
Label1.Text = 图片上传成功;
}
else
{
Label1.Text = Plz选择你的图像并上传;
}

}



不要忘记使用自己的连接字符串到服务器。


请看下面的链接

保存数据库asp.net中文件和路径中的图像 [ ^ ]





 受保护  void  Button1_Click( object  sender,EventArgs e)
{
if (FileUpload1.HasFile)
{
string theFileName = Path.Combine(Server.MapPath( 〜/ Uploads),FileUpload1.FileName);

SqlCommand cmd = new SqlCommand( 插入FileUploads值(' + TextBox1.Text + ',' + theFileName + '),con);
cmd.ExecuteNonQuery();
con.Close();
if (File.Exists(theFileName))
{
File.Delete(theFileName);
}
FileUpload1.SaveAs(theFileName);
Label1.Text = 图片上传成功;
}
else
{
Label1.Text = Plz选择你的图像并上传;
}

}



您可以查看:

如何获取路径,一篇好文章 [ ^ ]


uploading image in database and wants to show that image in gridview bt when uploading image i m getting an issue..help me/remove erroer plzz..



protected void Button1_Click(object sender, EventArgs e)
   {
       if (FileUpload1.HasFile)
       {
           String str = FileUpload1.FileName;
           FileUpload1.PostedFile.SaveAs(Server.MapPath(",") + "~//uploadedimages//" + str);
           string path = "~//Uploads//"+str.ToString();
           con.Open();
           SqlCommand cmd = new SqlCommand("insert into uploads values('" + TextBox1.Text + "','" + path + "')", con);
           cmd.ExecuteNonQuery();
           con.Close();
           Label1.Text = "Image upload Successfully";
       }
       else
       {
           Label1.Text = "Plz select ur image and upload";
       }

   }


error occoured when i use path as= "~//uploadedimages//"

Could not find a part of the path 'F:\Atverse\Web Example\Image\,~\uploadedimages\Chrysanthemum.jpg'.


error occoured when i use path as= ""~F:/Atverse/Web Example/Image/uploadedimages/"

The given path's format is not supported.


plzz tell me error..or right code for this.

解决方案

string
                filename = Path.GetFileName(fileupload1.FileName);
                fileupload1.SaveAs(Server.MapPath("~/")
                + filename);


Give it a try,Hope this will help.


Run this script in sql server management studio

CREATE TABLE [dbo].[FileUploads](
	[uid] [int] IDENTITY(1,1) NOT NULL,
	[uname] [nvarchar](max) NULL,
	[uimg] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


change the following line in the method:

SqlCommand cmd = new SqlCommand("insert into FileUploads values('" + TextBox1.Text + "','" + theFileName + "')", con);



Updated one:

protected void Button1_Click(object sender, EventArgs e)
        {
            string connectionString = "Put your connection string here";
            if (FileUpload1.HasFile)
            {
                string theFileName = Path.Combine(Server.MapPath("~/Uploads"), FileUpload1.FileName);

                using (var con = new SqlConnection(connectionString))
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("insert into uploads values('" + TextBox1.Text + "','" + theFileName + "')", con);
                    cmd.ExecuteNonQuery();
                }
                if (File.Exists(theFileName))
                {
                    File.Delete(theFileName);
                }
                FileUpload1.SaveAs(theFileName);
                Label1.Text = "Image upload Successfully";
            }
            else
            {
                Label1.Text = "Plz select ur image and upload";
            }

        }


Don't forget to use your own connection string to the server.


Please have a look on the below link
save image in file and path in database asp.net[^]


protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string theFileName = Path.Combine(Server.MapPath("~/Uploads"), FileUpload1.FileName);

                SqlCommand cmd = new SqlCommand("insert into FileUploads values('" + TextBox1.Text + "','" + theFileName + "')", con);
                cmd.ExecuteNonQuery();
                con.Close();
                if (File.Exists(theFileName))
                {
                    File.Delete(theFileName);
                }
                FileUpload1.SaveAs(theFileName);
                Label1.Text = "Image upload Successfully";
            }
            else
            {
                Label1.Text = "Plz select ur image and upload";
            }

        }


You can have a look upon :
How to get the path, a good article [^]


这篇关于图像从数据库上传和显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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