2016-07-28 12 views
3

こんにちはイメージアップロードのこのC#コードを使用しています。なぜ私はもう1人のキャラクターを手に入れたのですか?

protected void UploadFile(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[] bytes = br.ReadBytes((Int32)fs.Length); 
      string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; 
      using (MySqlConnection con = new MySqlConnection(constr)) 
      { 
       string query = "INSERT INTO foto(FileName, ContentType, Content) VALUES (@FileName, @ContentType, @Content)"; 
       using (MySqlCommand cmd = new MySqlCommand(query)) 
       { 
        cmd.Connection = con; 
        cmd.Parameters.AddWithValue("@FileName", filename); 
        cmd.Parameters.AddWithValue("@ContentType", contentType); 
        cmd.Parameters.AddWithValue("@Content", bytes); 
        con.Open(); 
        cmd.ExecuteNonQuery(); 
        con.Close(); 
       } 
      } 
     } 
    } 
    Response.Redirect(Request.Url.AbsoluteUri); 
} 

は、私はその後、私はC#のWebサービス、AJAX、JavaScriptを使用していた画像を表示Base64Stringに画像を変換しますが、画像が存在しない場合のように表示されているために、LONGBLOBフィールドに画像をアップロードします。ここで

は私Base64Stringです:

Base64String

あなたは問題がこの余分な文字である見ることができるよう:

AAEAAAD/////AQAAAAAAAAAPAQAAAHgBAAAC 

これが起こるのはなぜ?そして私はそれをどのように解決できますか?

http://jsfiddle.net/hpP45/ 

は私が解読しよう:私は間違っているか、ここに行かない場合、私がチェック

+1

これは、バイナリ形式のシリアル化ヘッダーです。これは奇数です。あなたはテーブルのデータを見ましたか?これらの先頭バイトは含まれていますか? ToBase64Stringコールの前にデータを読み込んで読み込むコードを表示する必要がない場合は、C#asp.netページでの表示用に –

答えて

0

 protected void UploadFile(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)) 
       { 
       System.Drawing.Image imagetuUpload = System.Drawing.Image.FromStream(fs); 

       Bitmap bitmap = new Bitmap(imagetuUpload); 
       System.IO.MemoryStream stream = new MemoryStream(); 
       bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); 
       stream.Position = 0; 
       byte[] upproimag = new byte[stream.Length + 1]; 
       stream.Read(upproimag, 0, upproimag.Length); 

       string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; 
       using (MySqlConnection con = new MySqlConnection(constr)) 
        { 
        string query = "INSERT INTO foto(FileName, ContentType, Content) VALUES (@FileName, @ContentType, @Content)"; 
        using (MySqlCommand cmd = new MySqlCommand(query)) 
         { 
         cmd.Connection = con; 
         cmd.Parameters.AddWithValue("@FileName", filename); 
         cmd.Parameters.AddWithValue("@ContentType", contentType); 
         cmd.Parameters.AddWithValue("@Content", upproimag); 
         con.Open(); 
         cmd.ExecuteNonQuery(); 
         con.Close(); 
         } 
        } 
       } 
      } 
     Response.Redirect(Request.Url.AbsoluteUri); 
     } 

私はそれはあなたを助けるかもしれホップあなたは上記のコードで蛇腹行を置き換えることができます:)

+0

のコードを使用する必要があります。imguser.ImageUrl = "data:image/jpg; base64" + Convert.ToBase64String( SQLブロブ); –

+0

イメージを呼び出す際に問題が発生しました。しかし助けてくれてありがとう –

0

長い時間戻って、私はその時点でこれを行う以外に覚えていない同じ問題にありました最初に(Google bse64デコーダ)

https://www.base64decode.org/ 

jpegファイルとしてデコードされたバイナリデータを保存して開きます。

それは、私が推測開かない場合は、お使いのbase64エンコードに問題がある可能性があります

関連する問題