asp.net中的文件上传控件 [英] File Uploading control in asp.net

查看:92
本文介绍了asp.net中的文件上传控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我用文件上传控件开始我的代码时,它已经全部结束了,但我必须与我的例外对抗

,而字符串或二进制数据的异常消息将被截断 。我无法知道哪些二进制数据会被截断

而且我还创建了一个名为INFORMATION的表,其列号为id,name,photo

query = 创建表信息(id int,name varchar(size),photo varbinary(max))

id =某个数字

名称 =上传文件的名称

照片 =上传文件的内容(例如: - > JPEG或PPG..ETC)

我还拿了一个文本框输入id no和一个 fileuploadcontrol 和一个按钮(上传)

这是我的前端代码

When i start my code with file upload control it's all over, but i have to fight with my exception
with exception message of string or binary data would be truncated.I cannot come to know which binary data would be truncated
and also i had created a table of name INFORMATION with columns id,name,photo
query=create table information (id int,name varchar(size),photo varbinary(max))
id=some number
name=name of the uploaded file
photo=content of uploaded file(eg:->JPEG OR PPG..ETC)
and also i had taken one textbox to enter id no and one fileuploadcontrol and one button(upload)
here is my front end code

protected void btnupload_Click(object sender, EventArgs e)
{
    string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
    string contenttype=FileUpload1.PostedFile.ContentType;
    using (Stream fs=FileUpload1.PostedFile.InputStream)
    {
        using (BinaryReader br=new BinaryReader(fs))
        {
         byte[] bt = br.ReadBytes((int)fs.Length);
         string constr = ConfigurationManager.ConnectionStrings["naveen"].ToString();
            using (SqlConnection con=new SqlConnection(constr))
            {
                string query = "insert into information values(@id,@name,@photo)";
                using (SqlCommand cmd=new SqlCommand(query))
                {
                    cmd.Connection = con;
                    cmd.CommandText = query;
                    cmd.Parameters.AddWithValue("@id", TextBox1.Text);
                    cmd.Parameters.AddWithValue("@name", filename);
                    cmd.Parameters.AddWithValue("@photo", contenttype);
                    con.Open();
                    int i=cmd.ExecuteNonQuery();----->Here is the statement which i have got exception
                    con.Close();
                    if (i>0)
                    {
                        Label1.Text = "File uploaded succesfully";
                    }
                    else
                    {
                        Label1.Text = "failed to upload";
                    }
                }
            }

        }
    }
}



和我该如何解决这个异常......:)


and how do i resolve this exception.... :)

推荐答案

引用:

photo =上传文件的内容(例如: - > JPEG或PPG..ETC)

photo=content of uploaded file(eg:->JPEG OR PPG..ETC)

将此列的数据类型更改为图像不是 varbinary(max)

Alter DataType of this Column to image not varbinary(max).


OK - 错误来自SQL,并告诉您已尝试插入的一个或多个数据元素进入信息表不适合提供的空间。

哪一个,我们无法从这里说出来,因为我们无法访问您的数据库或数据。



因此,请查看您的数据库设计,检查您允许的字段大小,并将它们与您提供的数据长度进行比较。可能是您的用户键入的id太长,或者文件名不合适,或者......



BTW:你为什么这样做?

1)用户指定ID?如果它已存在于DB中怎么办?他们浪费上传和你的应用程序崩溃。

2)你为什么要保存内容类型,而不是你花在阅读数组字节上的实际数据?
OK - the error is coming from SQL, and is telling you that one or more of the data elements you have tried to insert into the "information" table does not fit in the space provided.
Which one, we can't tell from here, because we don't have access to your DB or data.

So, look at your DB design, check teh field sizes you have allowed, and compare them against the length of the data you have provided. It could be that your user typed an "id" that is too long, or the file name doesn't fit, or...

BTW: why are you doing that?
1) The user specifies the ID? What if it exists in the DB already? they waste the upload and your app crashes.
2) Why are you saving the content type, but not the actual data you took such care to read into an array of bytes?


这篇关于asp.net中的文件上传控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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