简体   繁体   English

System.Byte,上传图片

[英]System.Byte , Upload picture

I have a little question .. I am using blob to upload my files and I can download them too when they are movies or mp3 for example, but when I try to download a photo i always have this exception ... Can someone tell me why ? 我有一个小问题..我正在使用blob上载我的文件,例如,当它们是电影或mp3时,我也可以下载它们,但是当我尝试下载照片时,我总是会遇到此异常...有人可以告诉我为什么呢?

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.

Description: An unhandled exception occurred during the execution of the current
web request. Please review the stack trace for more information about the error 
and where it originated in the code. 

Exception Details: System.InvalidCastException: Unable to cast object of type 
'System.DBNull' to type 'System.Byte[]'.

Source Error: 

 An unhandled exception was generated during the execution of the current web requt.
 Information regarding the origin and location of the exception can be identified using
 the exception stack trace below.

Thank you for helping. 感谢您的帮助。

    This is the code to retrieve them . 

       string filename = Request["file"].ToString();
        var conString = ConfigurationManager.ConnectionStrings["LocalSqlServer"];
        string strConnString = conString.ConnectionString;
        SqlConnection dbConnection = new SqlConnection(strConnString);
        dynamic queryString = ("SELECT Data,ContentType FROM Files WHERE Name = '" + filename + "'");
        SqlCommand theCommand = new SqlCommand(queryString, dbConnection);
        dbConnection.Open();
        SqlDataReader reader = theCommand.ExecuteReader();

        if (reader.Read() && reader != null)
        {

            Byte[] bytes;
            bytes = Encoding.UTF8.GetBytes(String.Empty);
            bytes = (Byte[])reader["Data"];
                            string contentType = reader["ContentType"].ToString();
            Response.ContentType = contentType;
            Response.AddHeader("content-disposition", "attachment;  filename=" + filename + "");
            Response.BinaryWrite(bytes);
            reader.Close();
        }

        dbConnection.Close();

And to upload them : 并上传它们:

   dbConnection.Open();
                        string queryString = "INSERT INTO Files (Name,Path,UserUpload,Date,Data,ContentType,Size) VALUES (@Name,@Path,@UserUpload,@Date,@Data,@ContentType,@Size);" + "SELECT CAST(scope_identity() AS int)";
                        SqlCommand theCommand = new SqlCommand(queryString, dbConnection);
                        theCommand.Parameters.AddWithValue("@Name", FileUpload1.FileName);
                        theCommand.Parameters.AddWithValue("@Path", GetTheCurrentDirectory(MyTreeView.SelectedNode));
                        theCommand.Parameters.AddWithValue("@UserUpload", Request.Cookies["UserSettings"]["UserName"]);
                        theCommand.Parameters.AddWithValue("@Date", DateTime.Now);
                        theCommand.Parameters.AddWithValue("@Data", FileUpload1.FileBytes);
                        theCommand.Parameters.AddWithValue("@ContentType", FileUpload1.PostedFile.ContentType);
                        theCommand.Parameters.AddWithValue("@Size", FileUpload1.PostedFile.ContentLength);

                        int newFid = (Int32)theCommand.ExecuteScalar();

                        dynamic queryStringFolder = "INSERT INTO FILES_FOLDERS (File_Id,Folder_Id) VALUES (@FileId,@FolderId);";
                        theCommand = new SqlCommand(queryStringFolder, dbConnection);
                        theCommand.Parameters.AddWithValue("@FileId", newFid);
                        theCommand.Parameters.AddWithValue("@FolderId", MyTreeView.SelectedValue);
                        theCommand.ExecuteNonQuery();

It sounds like the blob you are attempting to retrieve is NULL. 听起来您尝试检索的Blob为NULL。 If this is an expected condition, you'll need to place a check for DBNull before you attempt to cast to a byte array. 如果这是预期的情况,则在尝试强制转换为字节数组之前,需要对DBNull进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM