Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 系统。字节,上传图片_C#_Asp.net_Sql_Upload - Fatal编程技术网

C# 系统。字节,上传图片

C# 系统。字节,上传图片,c#,asp.net,sql,upload,C#,Asp.net,Sql,Upload,我有一个小问题。。我使用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 rev

我有一个小问题。。我使用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.
谢谢你的帮助

    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();
并上传:

   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();

听起来您试图检索的blob是空的。如果这是预期的情况,则在尝试强制转换到字节数组之前,需要检查DBNull。

但这很奇怪,因为在我的数据库中可以看到字节=(请提供有关您的场景的更多信息。您是如何从数据库检索数据的?如果您放置断点,在从数据库检索数据后,是否可以立即看到数据中的字节?感谢您发布详细信息。这很有帮助。我假设您在尝试以下操作时引发异常:字节=(Byte[])reader[“Data”];为了方便起见,您可以在select语句中添加以下内容:“WHERE Data NOT NULL”…我希望reader.Read()在该点返回false。如果是这样,请查看您的上载过程并确保数据被正确持久化。