我正在尝试将图像保存在我的 D: 目录中,为此,我保存了组件中的Session
一些信息FileUpload
。
在我调用的方法中,btnConfirm_Click
我创建了我的方法Session
,在我的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
{
}
}
}