0

D: ディレクトリに画像を保存しようとしています。これを達成するためにSession、コンポーネントからいくつかの情報を保存していFileUploadます。

btnConfirm_Click私のメソッドI create mySessionと呼ばれる方法で、btnSave_Clickこの情報を復元してファイルを保存しようとしましたが、D:ディレクトリをチェックインするとファイルは存在しますが、このファイルを開くと、次のメッセージが表示されました。The windows photo viewer can not open this picture because the file appears to be damaged, corrupted, or is too big ..

誰かが私を助けることができますか?

C# コード

protected void btnConfirm_Click(object sender, EventArgs e)
{
 if (FileUpload1.HasFile)
            {
                string sFileName = FileUpload1.FileName;
                string fileExtension = System.IO.Path.GetExtension(sFileName).ToLower();
                foreach (string ext in new string[] { ".jpeg", ".jpg", ".png" })
                {
                    if (fileExtension == ext)
                    {
                        Session["Document"] = sFileName + fileExtension;
                        Session["Byte"] = FileUpload1.FileBytes;
                        Session["Content"] = FileUpload1.FileContent;
                        byte[] b = (byte[])Session["Byte"];
                    }
                }
           }
}


protected void btnSave_Click(object sender, EventArgs e)
        {
                if (Session["Document"].ToString() != null)
                {
                    try
                    {
                        byte[] byteArray = Encoding.UTF8.GetBytes(Session["Content"].ToString());                        
                        MemoryStream stream = new MemoryStream(byteArray);

                        sPath = "D:/123.jpg";
                        FileStream fileStream = File.Create(sPath, (int)stream.Length);                        
                        byte[] bytesInStream = new byte[stream.Length];
                        stream.Read(bytesInStream, 0, bytesInStream.Length);                        
                        fileStream.Write(bytesInStream, 0, bytesInStream.Length);
                    }
                    catch
                    {
                    }
              }
         }
4

3 に答える 3