任何人请逐行解释此代码

任何人请逐行解释此代码

本文介绍了任何人请逐行解释此代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图多次将图像文件上传到MySql,但是我没有正确完成.所以最后我从CP获得了工作代码.它可以运行,但是我想逐步了解该过程.任何人都可以逐行解释以下代码吗?

I''m trying to upload an image file into MySql many times, but I did''t get it right. So finally I got working code from CP. It runs, but I want to understand the process step by step. Can anyone please explain the following code line by line?

    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.Files.Count != 0)
        {
            HttpPostedFile httpFile = Request.Files[0];
            string extension = this.GetFileExtension(httpFile.ContentType);
            if(extension == null )
            {
                Response.Write("Mime type not Supported");
                return;
            }
            BufferedStream bf = new BufferedStream(httpFile.InputStream);
            byte[] buffer = new byte[bf.Length];
            bf.Read(buffer,0,buffer.Length);
            DataAccess data = new DataAccess();
            data.addImage(buffer,extension);
            Response.Write("Image Added!");
        }
    }

    private string GetFileExtension(string p)
    {
        return p;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    }

    public class DataAccess
    {
        MySqlConnection con = new MySqlConnection("user id=root; password=admin; database=qms; server=localhost");
        public string addImage(byte[] buffer, string extension)
        {
            string strSql = "SELECT * FROM image";
            DataSet ds = new DataSet("Image");
            MySqlDataAdapter tempAP = new MySqlDataAdapter(strSql, this.con);
            MySqlCommandBuilder objCommand = new MySqlCommandBuilder(tempAP);
            tempAP.Fill(ds, "Table");

            try
            {
                this.con.Open();
                DataRow objNewRow = ds.Tables["Table"].NewRow();
                objNewRow["Extension"] = extension;
                objNewRow["Data"] = buffer;
                ds.Tables["Table"].Rows.Add(objNewRow);
                // trying to update the table to add the image
                tempAP.Update(ds, "Table");
            }
            catch (Exception e) { return e.Message; }
            finally { this.con.Close(); }
            return null;
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
    }

}



感谢您的帮助!



Thanks for your help!

推荐答案


if(Request.Files.Count != 0)


如果您不了解某些内容,并且其中包含."字符,则将其分解并使用google查看MSDN:
Request是一个对象,它检索客户端浏览器传递给服务器的值: [ ^ ]
因此,Request.Files是HttpRequest(这是Request的实例)属性: MSDN [ ^ ]
Count是所有Collections的标准属性,它告诉您其中包含多少个项目.

所以Request.Files.Count告诉您上传了多少文件.

如果该数字不为零,则立即在下面执行代码块.

(您现在看到了,解释一个完整的程序需要多少工作,即使是那么小的程序?:)


If you don''t understand something and it has ''.'' characters in it, then break it down and look at MSDN with google:
Request is an object which retrieves the values that the client browser passed to the server: MSDN[^]
So Request.Files is an HttpRequest (which is what Request is an instance of) property : MSDN[^]
And Count is a standard property of all Collections, which tells you how many items it contains.

So Request.Files.Count tells you how many files were uploaded.

If this number is not zero, then execute the code block immediately below.

(Do you see now, how much work is involved in explaining an entire program, even one that small? :)


这篇关于任何人请逐行解释此代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 07:15