1

だから私はこのコードを持っています:

if ((uplImage.FileName != ""))
{
    byte[] raw = new byte[10000];

    //to allow only jpg gif and png files to be uploaded.
    string extension = Path.GetExtension(uplImage.PostedFile.FileName.ToUpper());
    if (((extension == ".JPG") || ((extension == ".GIF") || (extension == ".PNG"))))
    {

        DALBio bio = new DALBio();

        FileStream fs = new FileStream(uplImage.PostedFile.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
        fs.Read(raw, 0, System.Convert.ToInt32(fs.Length));

        bio.PlayerID = Session["playerID"].ToString();
        bio.Pending = 'Y';
        bio.Photo = raw;
        DALBio.insertImage(bio);
    }
}

これを試してみると、ストリームが画像を読み取っていません。rawイメージは決して得られません。空のままで、ストアドプロシージャを実行すると、イメージを渡したことがないと言ってキャッチされます。コードは問題ないと確信しています。画像をバイト配列に取り込めない理由がわかりません。

4

2 に答える 2

0

私がすることは、私が得ることですByteArray

public Byte[] GetArrayFromFile(string path)
{
  Byte[] data = null;

  FileInfo fileInf = new FileInfo(path);
  long numBytes = fileInf.Length;

  using (FileStream fStream = new FileStream(path, FileMode.Open, FileAccess.Read))
  {
    BinaryReader bReader = new BinaryReader(fStream);

    data = bReader.ReadBytes((int)numBytes);
  }
  return data;
}

DAL次に、Entity Frameworkを使用してデータベースに正しく保存します(オブジェクトをデータベースに正しく挿入すると機能するはずです)。

于 2013-05-15T16:50:32.120 に答える
0

raw配列を次のように作成/定義できます

     FileStream fs = new FileStream(uplImage.PostedFile.FileName, 
                         FileMode.OpenOrCreate, FileAccess.ReadWrite, 
                          FileShare.Read);

    raw = new byte[fs.Length]; 

// same code as above..

同様のコードを試すこともできます

        System.IO.Stream myStream;
        Int32 fileLen;

        // Get the length of the file.
        fileLen = uplImage.PostedFile.ContentLength;  


        // Create a byte array to hold the contents of the file.
        Byte[] input = new Byte[fileLen];

        // Initialize the stream to read the uploaded file.
        myStream = uplImage.FileContent;

        // Read the file into the byte array.
        myStream.Read(input, 0, fileLen); 
        // input will hold the byte array
于 2013-05-15T17:14:58.570 に答える