簡體   English   中英

System.Byte,上傳圖片

[英]System.Byte , Upload picture

我有一個小問題..我正在使用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為NULL。 如果這是預期的情況,則在嘗試強制轉換為字節數組之前,需要對DBNull進行檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM