asp.net 上传图片并用img控件局部刷新显示
用到了一般处理程序(ashx)和aspx之间的传值上传控件。cs:
using System;
using System.Collections.Generic;
using System.Linq;
using
using
using System.Web;
using System.Web.Services;
using GoldenEasy.YangclGecmp.Data;
using System.Data.SqlClient;
using GECMP_IDAL;
namespace GECMP.Ajax
{
public partial class UploadFile : System.Web.UI.Page
{
DBOperator dbo = DBOperatorFactory.GetDBOperator();
protected void Page_Load(object sender, EventArgs e)
{
HttpPostedFile file = Request.Files["Filedata"];
string uploadPath =
HttpContext.Current.Server.MapPath(Request["folder"]) + "\\";
if (file != null)
{
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
file.SaveAs(uploadPath + file.FileName);
//下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失
string Path = "\\UploadFile\\";
string ImgName = file.FileName;
Session["ImageName"]= ImgName;
string ImagePath = Path + ImgName;
Session["ImgPath"] = ImagePath;
//原有的东西,和数据库操作没关系
Response.Write("1");
}
else
{
Response.Write("0");
}
}
}
}
页面的保存在一般处理程序中处理:
para[38] = "0.00";
para[39] = "0";
para[40] = context.Request["UnitName.id"];
switch (Title)
{
case "add":
OBJ.ExecProcReInt("pt_Insert_EstateReg", para);
string sql = "insert into HousePhoto(PriCode,ImgName,Note) Values('" + HouseCode + "','" + Imgname + "','" + ImgPath + "')";
OBJ.ExecSQLReInt(sql);
context.Response.Write(success("", "HouseApply"));
context.Response.Redirect("//HouseRegister/RegisterApply.aspx?s1=" + HouseCode + "");
break;
怎样达到我的效果啊,上传同时img局部刷新显示图片。
客户界面:
<td class="right_bg" colspan="5">
<a href="../Control/UploadFile.aspx" maxable="false" width="550" target="dialog"><span
style="color: Red;">添加附件</span></a>
</td>
</tr>
<tr>
<td class="right_bg" colspan="6" style="height: 100px;">
<p>
比例:</p>
<asp:Image ID="D21" runat="server" ImageUrl="\\UploadFile\Tulips.jpg" style="width: 900px; height: 220px;" /><br />
</td>
</tr>
</table>
界面。cs:
/// 显示图片
/// </summary>
public void ShowImage()
{
string HouseCode = Request.QueryString["s1"].ToString();
//string sql = "SELECT Note FROM HousePhoto where PriCode='HouseCode'";
string sql = "SELECT Note FROM HousePhoto where PriCode='1'";
SqlConnection con = new SqlConnection("Data Source=192.168.1.18;Initial Catalog=yjoa;User ID=sa;pwd=123;");
con.Open();
//SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
sda.Fill(ds);
//this.imgUrl = ds.ToString();
this.D21.ImageUrl = ds.Tables[0].Rows[0]["Note"].ToString();
//string wpath = ds.Tables[0].Rows[0]["wpath"].ToString();
//Image1.ImageUrl = wpath;
//Label3.Text = wpath;
con.Close();//关闭连接
con.Dispose();
}