filestream保存图片浏览问题
把图片用filestream保存到硬盘 能保存到但无法预览图片打开后什么都不显示
代码如下
改成了这样 还不对
public string SaveFile(byte[] binData,string imgurl)
{
string backmes = "";
string savePath = System.Web.HttpContext.Current.Server.MapPath("..\\" + imgurl.Substring(0, imgurl.LastIndexOf("\\") + 1));
if (!Directory.Exists(savePath))
{
return backmes = "图片保存路径不存在!请在根目录下创建您指定的文件夹!";
}
FileStream fileStream = null;
System.IO.BinaryWriter bw = null;
try
{
fileStream = new FileStream(savePath + imgurl.Substring(imgurl.LastIndexOf("\\")+1), FileMode.Create, FileAccess.Write);
//write the file
bw = new System.IO.BinaryWriter(fileStream);
bw.Write(binData, 0, binData.Length);
bw.Flush();//clear the buffer,write the data to the hard disk
backmes = "";
}
catch (Exception ex)
{
return ex.ToString();
}
finally
{
bw.Close();
}
return backmes;
}
保存的图片打开后显示不出来 无法预览
给个解决方式 给高分 --------------------编程问答-------------------- 这是我图片上传的代码.我的能够实现.图片存在本地硬盘.然后把:文件名.jpg存在数据库.其中文件名和数据库的是一样的.
private void Submit1_ServerClick(object sender, System.EventArgs e)
{
string path;
string FileName;
long FileSize;
string fType;
string ImageType ;
// ID=Convert.ToInt32 (Session["ID"].ToString ());
// UserName=Session["UserName"].ToString ();
if(File1.PostedFile.FileName.Trim ()!="")
{
if(this.txtTitle .Text =="" || this.txtRemark .Text =="")
{
Page.RegisterStartupScript("提示信息","<script>alert('请填写资料完整');</script>");
return;
}
try
{
//获取用户上传文件的信息
ImageType = Path.GetExtension(File1.PostedFile.FileName).ToUpper();
FileName = DateTime.Now.ToString("yyyyMMddhhmmss");
path = Server.MapPath(".") + "/UpImages/" + FileName + ImageType;
fType=File1.PostedFile.ContentType;
FileSize=File1.PostedFile .ContentLength;
if(fType.Substring (0,5)!="image")
{
//Page.RegisterStartupScript("提示信息","<script>alert('不是images文件!');</script>");
this.lblInfo.Visible=true;
this.lblInfo.Text ="不是Images文件!";
return;
}
//将用户上传文件保存到服务器上
string strFileName=FileName+ImageType;
File1.PostedFile.SaveAs(path);
StrSql="insert into Photo(Filername,Filesize,UserName,Remark,ClassID,Title) values('"+strFileName+"',"+FileSize+",'"+this.lblUserName.Text +"','"+this.txtRemark .Text +"',"+Session["ClassID"].ToString ()+",'"+this.txtTitle.Text +"')";
MyDataOperate.ExecuteTable(StrSql);
this.lblInfo.Visible=true;
this.lblInfo .Text="图片上传成功!";
}
catch(Exception ex)
{
Page.RegisterStartupScript("提示信息","<script>alert('ex.Message');</script>");
}
} --------------------编程问答-------------------- 郁闷 我要的是从二进制保存到硬盘的图片 不是直接上传啊 --------------------编程问答-------------------- 读的时候要转换 --------------------编程问答-------------------- int i = image.LastIndexOf("\\") + 1;
imageurl = "./images/" + image.Substring(i);
FileStream fStreamRead = new FileStream(image, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[1024];
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
FileStream fStreamWrite = new FileStream(str+"/demo/images/" + image.Substring(i), FileMode.Create, FileAccess.Write);
int end = 1;
while (end != 0)
{
end = fStreamRead.Read(buffer, 0, 1024);
fStreamWrite.Write(buffer, 0, 1024);
}
fStreamRead.Close();
fStreamWrite.Close(); --------------------编程问答-------------------- +1 --------------------编程问答-------------------- 顶4楼
补充:.NET技术 , C#