Visual Studio中的FileUpload控件并将图像保存到MySql数据库 [英] FileUpload control in visual studio and save the image to MySql database

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

问题描述

如何使用fileupload控件上传图像并将其保存在MySQL Workbench数据库的表中?以及如何建立连接. 表格的名称是invoicesfp

How to use fileupload control to upload an image and save it in a table in MySQL workbench database? and how to create the connection. the name of tabel is invoicesfp

public partial class Invoices : System.Web.UI.Page
{
    MySql.Data.MySqlClient.MySqlConnection conn ;
    MySql.Data.MySqlClient.MySqlCommand cmd;
    String querystr;

    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            //save image into database
            string str = FileUpload1.FileName;
            FileUpload1.PostedFile.SaveAs(Server.MapPath(".") + "//Invoices//" + str);
            string path = "~//Invoices//" + str.ToString();
            MySqlConnection conn = new MySqlConnection("Server=127.0.0.1;Database=admindb;Uid=root;Pwd=8888;");
            MySqlCommand cmd = new MySqlCommand("insert into invoicesfp values (@v1)", conn);
            conn.Open();
            cmd.Parameters.AddWithValue("v1", TextBox1.Text);

            cmd.ExecuteNonQuery();
            conn.Close();
            Label4.Text = "Image uploaded sucessfully";
        }
        else
        {
            Label4.Text = " Please, upload the image ";
        }
    }
}

推荐答案

所以这是我为将图像保存到根目录编写的代码,您必须在应用程序的根目录中创建一个文件夹,例如upload:

So here is the code i wrote for your saving images into your roots, you have to create a folder in your root of application for example upload:

            #region fileupload
            string fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
            string ret = Rename.ChangeName();
            string SaveLocation = Server.MapPath("Upload") + "\\" + ret;

            try
            {
                FileUpload1.PostedFile.SaveAs(SaveLocation);
            }
            catch (Exception ex)
            {
                if (ex is ArgumentNullException || ex is NullReferenceException)
                {
                    throw ex; 
                }
            }
            string PicAddress = "~/Upload/" + ret;

            #endregion

您会看到有一种更改名称的方法,假设您只想保存jpg文件,对于其他文件,您可以使用扩展名:

as you can see there is a method for change name, assuming you just want to save jpg files, for other files you can use extension:

    public static string ChangeName()
    {
        return Guid.NewGuid().ToString("N") + ".jpg";
    }

运行这些代码后,您就可以轻松地将PicAddress作为字符串保存到数据库中,因此请为其创建例如nvarchar字段.每当您要显示图像时,只需要将图像的地址引用到图像标签即可:

Simply after these code ran, you can easily save the PicAddress as a string into your database, so make for example nvarchar field for it. anytime you want to show the image you just need to reference the address of the image to your image tag:

<img src="~/Upload/etcetc.jpg">

这篇关于Visual Studio中的FileUpload控件并将图像保存到MySql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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