改成了这样 还不对
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; }
保存的图片打开后显示不出来 无法预览
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; }
保存的图片打开后显示不出来 无法预览
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.txtRe .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,Re,ClassID,Title) values('"+strFileName+"',"+FileSize+",'"+this.lblUserName.Text +"','"+this.txtRe .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>");
}
}